home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / mpatrol / info / mpatrol.info
Text File  |  2000-05-16  |  342KB  |  7,783 lines

  1. This is mpatrol.info, produced by makeinfo version 4.0 from
  2. mpatrol.texi.
  3.  
  4. INFO-DIR-SECTION Programming tools
  5. START-INFO-DIR-ENTRY
  6. * mpatrol: (mpatrol).           The mpatrol library.
  7. END-INFO-DIR-ENTRY
  8.  
  9.    This file documents mpatrol, a library for controlling and tracing
  10. dynamic memory allocations.
  11.  
  12.    Copyright (C) 1997-2000 Graeme S. Roy <graeme@epc.co.uk>
  13.  
  14.    This is edition 1.9 of the mpatrol manual for version 1.2.0, 16th
  15. May, 2000.
  16.  
  17.    Permission is granted to make and distribute verbatim copies of this
  18. manual provided the copyright notice and this permission notice are
  19. preserved on all copies.
  20.  
  21.    Permission is granted to copy and distribute modified versions of
  22. this manual under the conditions for verbatim copying, provided that
  23. the entire resulting derived work is distributed under the terms of a
  24. permission notice identical to this one.
  25.  
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that this permission notice may be stated in a
  29. translation approved by the Free Software Foundation.
  30.  
  31.    All product names mentioned in the documentation and source code for
  32. this library are the trademarks of their respective owners.
  33.  
  34. 
  35. File: mpatrol.info,  Node: Top,  Next: Foreword,  Up: (dir)
  36.  
  37. mpatrol
  38. *******
  39.  
  40.                                  _                     _
  41.                                 | |                   | |
  42.  ____ ___    _____     _____   _| |_   _____   ____   | |
  43. |  _ ~ _ \  |  __ \   / __  | |_   _| |  ___| / __ \  | |
  44. | | | | | | | |  | | | |  | |   | |   | |    | |  | | | |
  45. | | | | | | | |__| | | |__| |   | |   | |    | |__| | | |
  46. |_| |_| |_| |  ___/   \_____|   |_|   |_|     \____/  |_|
  47.             | |
  48.             |_|
  49.  
  50.    This document describes mpatrol, a library for controlling and
  51. tracing dynamic memory allocations.
  52.  
  53.    This is edition 1.9 of the mpatrol manual for version 1.2.0, 16th
  54. May, 2000.
  55.  
  56. * Menu:
  57.  
  58. * Foreword::                    Motivation and feedback.
  59. * Overview::                    Overview of mpatrol.
  60. * Features::                    Features of mpatrol.
  61. * Installation::                Building and installing.
  62. * Integration::                 Debugging existing programs.
  63. * Memory allocations::          Background.
  64. * Operating system support::    Additional background.
  65. * Using mpatrol::               Using the library.
  66. * Profiling::                   Memory allocation profiling.
  67. * Improving performance::       Life beyond mpatrol.
  68. * How it works::                Implementation details.
  69. * Examples::                    Examples of usage.
  70. * Tutorial::                    Complete example.
  71. * Functions::                   Library functions.
  72. * Environment::                 Library run-time options.
  73. * Options::                     Command line options.
  74. * Library performance::         Comparitive timings.
  75. * Profiling file format::       Profiling output file layout.
  76. * Supported systems::           Known platforms that mpatrol runs on.
  77. * Notes::                       Known bugs and future enhancements.
  78. * Related software::            Similar programs.
  79. * Function index::              Library functions.
  80. * Index::                       Main index.
  81.  
  82. 
  83. File: mpatrol.info,  Node: Foreword,  Next: Overview,  Prev: Top,  Up: Top
  84.  
  85. Foreword
  86. ********
  87.  
  88.    I first started writing this library a few years ago when the
  89. company I work for sent me out to a customer who had reported a memory
  90. leak, which he expected was coming from the code generated by our C++
  91. compiler.  A few years on and the library has changed dramatically from
  92. its first beginnings, but I thought I'd release it publicly in case
  93. anyone else found it useful.
  94.  
  95.    When writing the library, I placed more emphasis on the quantity and
  96. quality of information about allocated memory rather than the speed and
  97. efficiency of allocating the actual memory.  This means that the
  98. library will use dramatically more memory than normal dynamic memory
  99. allocation libraries and can slow down to a crawl depending on which
  100. options you use.  However, the end results are likely to be accurate
  101. and reliable, and in most cases the library will run quite happily at a
  102. sane speed.
  103.  
  104.    The mpatrol library is by no means the only library of its kind.
  105. Solaris 7 has no less that 6 different malloc libraries, and there are
  106. plenty available as freeware or as commercial products.  Try to keep in
  107. mind that mpatrol comes with absolutely no warranty and so if it
  108. doesn't work for you and you need a fast solution, try some of the
  109. other libraries or products available.  I have listed some of the most
  110. popular at the end of this manual (*note Related software::).
  111.  
  112.    This manual is arranged so that complete reference material on the
  113. mpatrol library can be found in the appendices, while introductory and
  114. background material can be found in the preceding chapters and
  115. sections.  For readers who wish to delve right in and use the library,
  116. the Installation (*note Installation::) and Examples (*note Examples::)
  117. chapters should be enough to get started in combination with the quick
  118. reference card.  Otherwise, this manual should be read from beginning
  119. to end in order to get the most out of the software it describes.
  120.  
  121.    Due to their very nature, problems with dynamic memory allocations
  122. are notoriously difficult to reproduce and debug, and this is likely to
  123. be the case if you find a bug in the mpatrol library as it might be
  124. extremely hard to reproduce on another system.  Details on how to
  125. report bugs are given elsewhere in this document (*note Notes::), but
  126. it would be very useful if you could try to provide as much information
  127. as possible when reporting a problem, and that includes having a look
  128. in the library source code to see if it's obvious what is wrong.
  129. However, please try to read the FAQ first in case your question or
  130. problem is covered there since it is usually updated every time I
  131. receive a question about mpatrol.
  132.  
  133.    The latest version of the mpatrol library and this manual can always
  134. be found at `http://www.cbmamiga.demon.co.uk/mpatrol/', and any
  135. correspondence relating to mpatrol (bug reports, enhancement requests,
  136. compliments, etc.) should be sent to <mpatrol@cbmamiga.demon.co.uk>.
  137. The mpatrol library is also registered at FreshMeat
  138. (`http://freshmeat.net/') so you can receive notification of updates
  139. there as well.  I normally only check my e-mail about once or twice a
  140. week, so don't expect an immediate response.  I can also be reached at
  141. <graeme@epc.co.uk> but that is my work e-mail address.  There is now
  142. also a discussion group at `http://www.egroups.com/group/mpatrol/'
  143. where you can post mpatrol-related questions but you must first
  144. subscribe to the group before you can send mail to it.
  145.  
  146.    Note that this manual is not just intended to instruct readers on
  147. how to use the mpatrol library -- it is also written to give a detailed
  148. look at how malloc libraries work in general and how to improve the
  149. efficiency of existing code which uses them.  If this subject interests
  150. you, you may find further useful material at _The Memory Management
  151. Reference_ located at `http://www.harlequin.com/mm/reference/'.  It has
  152. links to many documents and research papers in the field of memory
  153. management, and has a large glossary which lists and explains related
  154. terms.  You may also wish to look at _A Memory Allocator_ by Doug Lea
  155. for information on general memory allocation principles.  It is located
  156. at `http://gee.cs.oswego.edu/dl/html/malloc.html'.
  157.  
  158.    Finally, I'd like to thank Stephan Springl (<springl@bfw-online.de>)
  159. for his help on reading debugging information from object files via the
  160. GNU BFD library, and Dave Gibson (<david@epc.co.uk>) for his help on
  161. writing thread-safe code.  Calum Wilkie (<calum@epc.co.uk>) also
  162. deserves a mention since the idea for providing stack traces comes from
  163. a similar library he wrote a few years ago.
  164.  
  165.    Oh, and always remember to do final release builds without the
  166. mpatrol library as the library is much slower than normal malloc
  167. implementations and uses much more memory.
  168.  
  169.    Happy debugging!
  170.  
  171.    Graeme Roy, 11th October, 1999.
  172.  
  173.    Edinburgh, Scotland.
  174.  
  175. 
  176. File: mpatrol.info,  Node: Overview,  Next: Features,  Prev: Foreword,  Up: Top
  177.  
  178. Overview
  179. ********
  180.  
  181.    The mpatrol library is yet another link library that attempts to
  182. diagnose run-time errors that are caused by the wrong use of
  183. dynamically allocated memory.  If you don't know what the `malloc()'
  184. function or `operator new[]' do then this library is probably not for
  185. you.  You have to have a certain amount of programming expertise and a
  186. knowledge of how to run a command line compiler and linker before you
  187. should attempt to use this.
  188.  
  189.    Along with providing a comprehensive and configurable log of all
  190. dynamic memory operations that occurred during the lifetime of a
  191. program, the mpatrol library performs extensive checking to detect any
  192. misuse of dynamically allocated memory.  All of this functionality can
  193. be integrated into existing code through the inclusion of a single
  194. header file at compile-time.  On UNIX and Windows platforms (and
  195. AmigaOS when using `gcc') this may not even be necessary as the mpatrol
  196. library can be linked with existing object files at link-time or, on
  197. some platforms, even dynamically linked with existing programs at
  198. run-time.
  199.  
  200.    All logging and tracing output from the mpatrol library is sent to a
  201. separate log file in order to keep its diagnostics separate from any
  202. that the program being tested might generate.  A wide variety of
  203. library settings can also be changed at run-time via an environment
  204. variable, thus removing the need to recompile or relink in order to
  205. change the library's behaviour.
  206.  
  207.    A file containing a summary of the memory allocation profiling
  208. statistics for a particular program can be produced by the mpatrol
  209. library.  This file can then be read by a profiling tool which will
  210. display a set of tables based upon the accumulated data.  The profiling
  211. information includes summaries of all of the memory allocations listed
  212. by size and the function that allocated them and a list of memory leaks
  213. with the call stack of the allocating function.
  214.  
  215.    The mpatrol library has been designed with the intention of
  216. replacing calls to existing C and C++ memory allocation functions as
  217. seamlessly as possible, but in many cases that may not be possible and
  218. slight code modifications may be required.  However, a preprocessor
  219. macro containing the version of the mpatrol library is provided for the
  220. purposes of conditional compilation so that release builds and debug
  221. builds can be easily automated.
  222.  
  223. 
  224. File: mpatrol.info,  Node: Features,  Next: Installation,  Prev: Overview,  Up: Top
  225.  
  226. Features
  227. ********
  228.  
  229.    An overall list of features contained in the mpatrol library is
  230. given below.  This is not intended to be exhaustive since the best way
  231. to see what the library does is to read the documentation and try it
  232. out.
  233.  
  234.    * Written for UNIX, AmigaOS, Windows and Netware platforms.
  235.  
  236.    * Can be built to allocate memory from a fixed-sized static array
  237.      rather than using heap memory from the system.
  238.  
  239.    * Can be built as archive, shared and/or thread-safe libraries on
  240.      systems that support them, or even as one large object file.  A
  241.      lint library can also be built from the mpatrol library on UNIX
  242.      platforms.
  243.  
  244.    * Details of memory allocations and free memory are stored
  245.      internally as a tree structure for speed and also to allow the
  246.      best fit allocation algorithm to be used.  This also enables the
  247.      library to perform intelligent resizing of memory allocations and
  248.      can be used to quickly determine if an address has been allocated
  249.      on the heap.
  250.  
  251.    * Contains 14 replacement C dynamic memory allocation functions:
  252.  
  253.      `malloc()'    ANSI    Allocates memory.
  254.      `calloc()'    ANSI    Allocates zero-filled memory.
  255.      `memalign()'  UNIX    Allocates memory with a specified alignment.
  256.      `valloc()'    UNIX    Allocates page-aligned memory.
  257.      `pvalloc()'   UNIX    Allocates a number of pages.
  258.      `strdup()'    UNIX    Duplicates a string.
  259.      `strndup()'   old     Duplicates a string with a maximum length.
  260.      `strsave()'   old     Duplicates a string.
  261.      `strnsave()'  old     Duplicates a string with a maximum length.
  262.      `realloc()'   ANSI    Resizes memory.
  263.      `recalloc()'  old     Resizes memory allocated by `calloc()'.
  264.      `expand()'    old     Resizes memory but does not relocate it.
  265.      `free()'      ANSI    Frees memory.
  266.      `cfree()'     old     Frees memory allocated by `calloc()'.
  267.  
  268.    * Contains 4 replacement C++ dynamic memory allocation functions:
  269.  
  270.      `operator new'       Allocates memory.
  271.      `operator new[]'     Allocates memory for an array.
  272.      `operator delete'    Frees memory.
  273.      `operator delete[]'  Frees memory allocated by `operator new[]'.
  274.  
  275.    * Contains 10 replacement C memory operation functions:
  276.  
  277.      `memset()'    ANSI    Fills memory with a specific byte.
  278.      `bzero()'     UNIX    Fills memory with the zero byte.
  279.      `memccpy()'   UNIX    Copies memory up to a specific byte.
  280.      `memcpy()'    ANSI    Copies non-overlapping memory.
  281.      `memmove()'   ANSI    Copies possibly-overlapping memory.
  282.      `bcopy()'     UNIX    Copies possibly-overlapping memory.
  283.      `memcmp()'    ANSI    Compares two blocks of memory.
  284.      `bcmp()'      UNIX    Compares two blocks of memory.
  285.      `memchr()'    ANSI    Searches memory for a specific byte.
  286.      `memmem()'    UNIX    Searches memory for specific bytes.
  287.  
  288.    * All of the above functions can also be defined with an additional
  289.      underscore prepended to their external name in order to catch all
  290.      uses of these functions in the system and third-party libraries.
  291.  
  292.    * Contains support for a user-defined low-memory handler function,
  293.      including a replacement for the C++ function, `set_new_handler()'.
  294.  
  295.    * Contains support for user-defined prologue and epilogue callback
  296.      functions, which get called before and after every memory
  297.      allocation, reallocation or deallocation.
  298.  
  299.    * A function is provided to return as much information as possible
  300.      about a given memory allocation, and can be called at any time
  301.      during program execution.  A similar function is also provided for
  302.      calling from within a debugger and an example command file is
  303.      provided for use with `gdb'.
  304.  
  305.    * A function is provided to display library settings and heap usage
  306.      statistics, including peak memory usage.  This information is also
  307.      displayed at program termination.
  308.  
  309.    * The library reads any user-controllable options at run-time from
  310.      an environment variable, but this does not have to be set as
  311.      defaults will then be used.  This prevents having to recompile
  312.      anything in order to change any library settings.  An option
  313.      exists to display a quick-reference summary of all of the
  314.      recognised options to the standard error file stream.
  315.  
  316.    * All diagnostics and logging are sent to a file in the current
  317.      directory, but this can be overridden, including forcing the log
  318.      file to be the standard output or standard error file streams.
  319.  
  320.    * Options exist to log details of every memory allocation,
  321.      reallocation or deallocation when they occur.
  322.  
  323.    * Options exist to halt the program at a specific memory allocation,
  324.      reallocation or deallocation when running the program within a
  325.      debugger.  These options have no effect when running the program
  326.      without a debugger.
  327.  
  328.    * An option exists to enable memory allocation profiling, which
  329.      forces a summary of all memory allocation statistics to be written
  330.      to a specified file for later use by a profiling command.  The
  331.      profiling file can also be written at a specified frequency.
  332.  
  333.    * A profiling command is provided which reads a profiling output
  334.      file produced by the mpatrol library and displays a set of tables
  335.      based on the accumulated data.  The profiling information includes
  336.      summaries of all of the memory allocations listed by size and the
  337.      function that allocated them and a list of memory leaks with the
  338.      call stack of the allocating function.
  339.  
  340.    * On UNIX platforms, the `mmap()' function can optionally be used to
  341.      allocate memory instead of the `sbrk()' function, but only if the
  342.      system supports it.  This can be useful if the mpatrol library
  343.      clashes with another malloc library that uses `sbrk()' to allocate
  344.      heap memory.
  345.  
  346.    * On non-UNIX platforms where the mpatrol library overrides
  347.      `malloc()' without requiring the inclusion of `mpatrol.h',
  348.      versions of the UNIX functions `brk()' and `sbrk()' are provided
  349.      for compatibility with certain libraries.  These should _not_ be
  350.      called by user code as they have only limited functionality.
  351.  
  352.    * All newly-allocated memory that is not allocated by the `calloc()'
  353.      or `recalloc()' functions will be pre-filled with a non-zero value
  354.      in order to catch out programs that wrongly assume that all
  355.      newly-allocated memory is zeroed.  This value can be modified at
  356.      run-time.
  357.  
  358.    * Can automatically check to see if there have been any illegal
  359.      writes to bytes located just before and after every memory
  360.      allocation through the use of overflow buffers.  The size of such
  361.      overflow buffers and the value to pre-fill them with can be
  362.      modified at run-time.  The checks will be performed before every
  363.      memory allocation call to ensure that nothing has overwritten the
  364.      overflow buffers, but a function is also provided to perform
  365.      additional checks under the programmer's control and an option
  366.      exists to specify a range in which checks will be performed.
  367.  
  368.    * On systems that support them, watch point areas can be used
  369.      instead of overflow buffers so that every read and write to memory
  370.      is checked to ensure that it is not within an overflow buffer.
  371.  
  372.    * Can automatically check to see if there have been any illegal
  373.      writes to free memory blocks.  The value to pre-fill free memory
  374.      blocks with can be modified at run-time.  The check will be
  375.      performed before every memory allocation call to ensure that
  376.      nothing has overwritten the free memory block, but a function is
  377.      also provided to perform additional checks under the programmer's
  378.      control and an option exists to specify a range in which checks
  379.      will be performed.
  380.  
  381.    * On systems that support memory protection, every memory allocation
  382.      can optionally be allocated at least one page of memory.  That
  383.      way, any free memory blocks can be made read and write protected
  384.      so that nothing can access free memory on the heap.  An option is
  385.      provided to specify whether all memory allocations should be
  386.      allocated at the start or at the end of such pages, and the bytes
  387.      left over within the pages become overflow buffers.
  388.  
  389.    * All freed memory allocations can optionally be prevented from
  390.      being returned to the free memory pool.  This is useful for
  391.      detecting if use is being made of freed memory just after a memory
  392.      allocation has been freed.  The contents of the memory allocation
  393.      can either be preserved or can be pre-filled with a value in order
  394.      to detect illegal writes to the freed memory allocation.
  395.  
  396.    * Calls to memory operation functions (such as `memset()' or
  397.      `memcpy()') have their arguments checked to ensure that they do
  398.      not pass null pointers or attempt to read or write memory
  399.      straddling the boundary of a previously allocated memory block,
  400.      although an option exists to turn such an error into a warning so
  401.      that the operation can still be performed.  Tracing from all such
  402.      functions can also optionally be written to the log file.
  403.  
  404.    * The internal data structures used by the library are kept separate
  405.      from the rest of the memory allocations.  On systems that support
  406.      memory protection, all of these internal data structures will be
  407.      write-protected in order to prevent corruption by the calling
  408.      program.  This feature can be overridden at run-time as it can
  409.      slow the program down.
  410.  
  411.    * Certain signals can be saved and restored on entry to each library
  412.      function and `errno' is set to `ENOMEM' if memory cannot be
  413.      allocated.
  414.  
  415.    * On systems that support memory protection, the library attempts to
  416.      detect any illegal memory accesses and display as much information
  417.      as it can obtain about the address in question and where the
  418.      illegal memory access occurred.
  419.  
  420.    * A call stack traceback from any function performing a memory
  421.      allocation is stored if the library supports this feature on the
  422.      system it is being run on.  This information can then be displayed
  423.      when information about a specific memory allocation is required.
  424.      Two different call stack traceback implementations are provided.
  425.  
  426.    * Symbol table details from executable files and shared libraries are
  427.      automatically read on systems that support this feature in order
  428.      to make the call stack tracebacks more meaningful.  An option also
  429.      exists to display a complete list of the symbols that were read by
  430.      the library at program termination.
  431.  
  432.    * Compiler-generated line number tables from any debugging sections
  433.      that exist in executable files and shared libraries can also be
  434.      used by the mpatrol library in order to provide more meaningful
  435.      information in call stack tracebacks.
  436.  
  437.    * If the library is unable to automatically determine a program's
  438.      executable filename to read symbols from then an option exists to
  439.      specify the full path to the program's executable file.
  440.  
  441.    * An option exists to change the default alignment used for
  442.      general-purpose memory allocations.
  443.  
  444.    * Contains support for a user-defined limit to available memory
  445.      which can be useful for stress-testing a program in simulated low
  446.      memory conditions.
  447.  
  448.    * Contains a feature to randomly fail a specific frequency of memory
  449.      allocations which can be useful for stress-testing error recovery
  450.      code in a program.
  451.  
  452.    * An option exists to display a complete memory map of the heap at
  453.      program termination.  A function to do this is also available to
  454.      call at any point during program execution.
  455.  
  456.    * Options exist to display all freed and unfreed memory allocations
  457.      at program termination in order to detect memory leaks.  A
  458.      separate program is also provided for locating memory leaks in
  459.      unfinished log files.
  460.  
  461.    * An option exists to abort the program with a failure condition if
  462.      there are more than a specified number of unfreed memory
  463.      allocations at program termination.  This could be useful for
  464.      batch testing in order to check that all tests free up most of
  465.      their allocated memory.
  466.  
  467.    * Functions always report if their arguments are illegal in order to
  468.      pinpoint any errors, and options exist to perform rigorous
  469.      checking of arguments when allocating, reallocating and freeing
  470.      memory.  In addition, checking is performed to ensure that memory
  471.      allocated by `operator new[]' is not freed with `free()' for
  472.      example.
  473.  
  474.    * The type of function performing a memory allocation is always
  475.      stored along with the allocation, as well as the file and line
  476.      number it was called from.  If compiled with `gcc', the function
  477.      name will also be stored and the thread identifier will be stored
  478.      if using the thread-safe library.
  479.  
  480.    * The library uses a header file to redefine the memory allocation
  481.      functions as macros in order to obtain more information about
  482.      where they were called from.  This is not strictly required on
  483.      UNIX and Windows platforms (and AmigaOS when using `gcc'), since
  484.      the library automatically redefines the default system memory
  485.      allocation functions.  All redefinitions in the header can also be
  486.      disabled by defining the `NDEBUG' preprocessor macro.
  487.  
  488.    * A command is supplied to run a program that was linked with the
  489.      mpatrol library with any specified options on the command line.
  490.      On some UNIX platforms, an option also exists to override the
  491.      default memory allocation routines for any dynamically-linked
  492.      program that was not previously linked with the mpatrol library.
  493.  
  494.    * The mpatrol library can be built to liaise with Parasoft Inuse, a
  495.      commercial graphical memory usage tool that can display the
  496.      current memory map of a running process.  Inuse is supplied with
  497.      Parasoft Insure++.
  498.  
  499.    * A small test suite is provided in order to test basic features.
  500.  
  501.    * User documentation is currently available in TeXinfo format as
  502.      well as UNIX manual pages and a quick reference card.
  503.  
  504. 
  505. File: mpatrol.info,  Node: Installation,  Next: Integration,  Prev: Features,  Up: Top
  506.  
  507. Installation
  508. ************
  509.  
  510.    The mpatrol library was initially developed on an Amiga 4000/040
  511. running AmigaOS 3.1.  I then installed RedHat Linux 5.1 on my Amiga and
  512. added support for Linux/m68k.  I've tried my best to make it as easy as
  513. possible to build and install mpatrol on any system, but it isn't
  514. likely to run smoothly for everybody.  However, there shouldn't be any
  515. major problems if you perform the following steps.
  516.  
  517.   1. Go into the `build' directory and then into the appropriate
  518.      subdirectory for your system.
  519.  
  520.   2. Edit the `Makefile' in that directory and check that it is using
  521.      the appropriate compiler and build tools.  The `CC' macro
  522.      specifies the compiler, the `AR' macro specifies the tool used to
  523.      build the archive library and the `LD' macro specifies the tool to
  524.      build the shared library.  The `CFLAGS' macro specifies compiler
  525.      options that are always to be used, the `OFLAGS' macro specifies
  526.      optimisation options for the compiler, the `SFLAGS' macro
  527.      specifies options to be passed to the compiler when building a
  528.      shared library and the `TFLAGS' macro specifies options to be
  529.      passed to the compiler when building a thread-safe library.  You
  530.      may also need to change the library names and library build
  531.      commands on different systems.
  532.  
  533.   3. Use the `make' command (or equivalent) to build the mpatrol
  534.      library in archive form.  The `all' target builds all possible
  535.      combinations of the mpatrol library for your system.  The `clean'
  536.      target removes all relevant object files from the current
  537.      directory, while the `clobber' target also removes all libraries
  538.      that have been built from the current directory.  On some UNIX
  539.      platforms, the `lint' target will build a `lint' library for the
  540.      mpatrol library.
  541.  
  542.   4. If the mpatrol library is to be built with support for Parasoft
  543.      Inuse then the `MP_INUSE_SUPPORT' preprocessor macro must be
  544.      defined in the `CFLAGS' portion of the `Makefile' before building.
  545.      This will ensure that Inuse will be notified of every memory
  546.      allocation, reallocation and deallocation, but the Insure++
  547.      runtime library will also have to be linked in with any program
  548.      that uses mpatrol.
  549.  
  550.   5. Copy all of the libraries that have been built into your local
  551.      library directory.  If there were symbolic links created in the
  552.      `build' directory then these should be recreated in the local
  553.      library directory rather than simply copying them.
  554.  
  555.   6. Copy the `mpatrol', `mprof' and `mleak' programs that have been
  556.      built into your local bin directory.
  557.  
  558.   7. Go up two directory levels into the `src' directory and copy the
  559.      `mpatrol.h' header file into your local include directory.
  560.  
  561.   8. On UNIX platforms, go up one directory level into the `man'
  562.      directory and copy the `man1' and `man3' subdirectories to your
  563.      local man directory.  Unfortunately, the location for manual pages
  564.      varies from system to system so you may or may not also be able to
  565.      copy the `cat1' and `cat3' subdirectories as well.  The `man*'
  566.      subdirectories contain the unformatted manual pages while the
  567.      `cat*' subdirectories contain the formatted manual pages.
  568.  
  569.   9. Go up one directory level into the `doc' directory and examine the
  570.      files located there.  The `mpatrol.texi' file contains the TeXinfo
  571.      source for this manual and can be translated into a wide variety
  572.      of documentation formats.  The `refcard.tex' file contains the
  573.      LaTeX source for the quick reference card and can be translated
  574.      into formats suitable for printing onto a single page.  There may
  575.      already be translated files in the `doc' directory, but if not you
  576.      will either have to generate them yourself using an appropriate
  577.      tool or you could download an archive containing the latest
  578.      mpatrol manual and reference card in a variety of documentation
  579.      formats from the mpatrol home page.  You can then install or print
  580.      these documents.
  581.  
  582.    Alternatively, the `pkg' directory contains files that can be used to
  583. automatically generate a _package_ in a specific format suitable for
  584. installation on a system.  Two package formats (PKG and RPM) and two
  585. archive formats are currently supported (generic tape archive and LhA).
  586. The first package format is generally used on UNIX SVR4 systems, while
  587. the second was introduced by Red Hat for use in their Linux
  588. distributions.  The generic tape archive can be used as a distribution
  589. for UNIX systems where no package format is supported, but it does not
  590. contain information on how to install the files on the system once they
  591. have been extracted from the distribution.  The LhA format is roughly
  592. the same, but is intended for Amiga systems and is used for Aminet
  593. distributions.  You should really know what you are doing before you
  594. attempt to build a package, and you should also be aware that some of
  595. the package files may need to be modified before you begin.
  596.  
  597. 
  598. File: mpatrol.info,  Node: Integration,  Next: Memory allocations,  Prev: Installation,  Up: Top
  599.  
  600. Integration
  601. ***********
  602.  
  603.    The following steps should allow you to easily integrate the mpatrol
  604. library into an existing application, although some of them may not be
  605. available to do on many platforms.  They are listed in the order of
  606. number of changes required to modify existing code -- the last step
  607. will require a complete recompilation of all your code.
  608.  
  609.   1. This step is currently only available on IRIX, Linux and Solaris
  610.      platforms and on DG/UX 4.20MU07 or later platforms with the
  611.      `LD_PRELOAD' feature.
  612.  
  613.      If your program or application has been dynamically linked with
  614.      the system C library (`libc.so') or an alternative malloc shared
  615.      library then you can use the `-d' option to the `mpatrol' command
  616.      to override the default definitions of `malloc()', etc. at
  617.      run-time without having to relink your program.
  618.  
  619.      For example, if your program's executable file is called
  620.      `testprog' and it accepts an option specifying an input file, you
  621.      can force the system's dynamic linker to use mpatrol's versions of
  622.      `malloc()', etc. instead of the default versions by typing:
  623.  
  624.           mpatrol -d ./testprog -i file
  625.  
  626.      The resulting log file should be called `mpatrol.<procid>.log' by
  627.      default (where PROCID is the current process id), but if no such
  628.      file exists after running the `mpatrol' command then it will not
  629.      be possible to force the run-time linking of mpatrol functions to
  630.      your program and you will have to proceed to the next step.
  631.  
  632.   2. This step is currently only available on UNIX and Windows
  633.      platforms (and AmigaOS when using `gcc').
  634.  
  635.      You should be able to link in the mpatrol library when linking
  636.      your program without having to recompile any of your object files
  637.      or libraries, but this will only be worthwhile on systems where
  638.      stack tracebacks are supported, otherwise you should proceed to
  639.      the next step since there will not be enough information for you
  640.      to tell where the calls to dynamic memory allocation functions took
  641.      place.
  642.  
  643.      Information on how to link the mpatrol library to an application
  644.      is given at the start of the examples (*note Examples::), but you
  645.      should note that if your program does not directly call any of the
  646.      functions in the mpatrol library then it will not be linked in and
  647.      you will not see a log file being generated when you run it.  You
  648.      can force the linking of the mpatrol library by causing `malloc()'
  649.      to be undefined on the link line, usually through the use of the
  650.      `-u' linker option.
  651.  
  652.   3. All of the following steps will require you to recompile some or
  653.      all of your code so that your code calls dynamic memory allocation
  654.      functions from the mpatrol library rather than the system C
  655.      library.
  656.  
  657.      For this step, if you have a rough idea of where the function
  658.      calls lie that you would like to trace or test, you need only
  659.      recompile the relevant source files.  You should modify these
  660.      source files to include the `mpatrol.h' header file before any
  661.      calls to dynamic memory allocation or memory operation functions.
  662.  
  663.      However, you should take particular care to ensure that all calls
  664.      to memory allocation functions in the mpatrol library will be
  665.      matched by calls to memory reallocation or deallocation functions
  666.      in the mpatrol library, since if they are unmatched then the log
  667.      file will either fill up with errors complaining about trying to
  668.      free unknown allocations, or warnings about unfreed memory
  669.      allocations at the end of execution.
  670.  
  671.   4. This step requires you to recompile all of your source files to
  672.      include the `mpatrol.h' header file.  Obviously, this will take
  673.      the longest amount of time to integrate, but need not require you
  674.      to change any source files if the compiler you are using has a
  675.      command line option to include a specific header file before any
  676.      source files.
  677.  
  678.      For example, `gcc' comes with a `-include' option which has this
  679.      feature, so if you had to recompile a source file called `test.c'
  680.      then the following command would allow you to include `mpatrol.h'
  681.      without having to modify the source file:
  682.  
  683.           gcc -include /usr/local/include/mpatrol.h -c test.c
  684.  
  685.    In all cases, it will be desirable to compile your source files with
  686. compiler-generated debugging information since that may be able to be
  687. used by the `USEDEBUG' option.  In addition, more symbolic information
  688. will be available if the executable files have not had their symbol
  689. tables stripped from them, although mpatrol can also fall back to using
  690. the dynamic symbol table from dynamically linked executable files.
  691.  
  692. 
  693. File: mpatrol.info,  Node: Memory allocations,  Next: Operating system support,  Prev: Integration,  Up: Top
  694.  
  695. Memory allocations
  696. ******************
  697.  
  698.    In the C and C++ programming languages there are generally three
  699. different types of memory allocation that can be used to hold the
  700. contents of variables.  Other programming languages such as Pascal,
  701. BASIC and FORTRAN also support some of these types of allocation,
  702. although their implementations may be slightly different.
  703.  
  704. * Menu:
  705.  
  706. * Static memory allocations::   Fixed location, fixed size.
  707. * Stack memory allocations::    Variable location, fixed size.
  708. * Dynamic memory allocations::  Variable location, variable size.
  709.  
  710. 
  711. File: mpatrol.info,  Node: Static memory allocations,  Next: Stack memory allocations,  Up: Memory allocations
  712.  
  713. Static memory allocations
  714. =========================
  715.  
  716.    The first type of memory allocation is known as a _static memory
  717. allocation_, which corresponds to file scope variables and local static
  718. variables.  The addresses and sizes of these allocations are fixed at
  719. the time of compilation(1) and so they can be placed in a fixed-sized
  720. data area which then corresponds to a section within the final linked
  721. executable file.  Such memory allocations are called static because
  722. they do not vary in location or size during the lifetime of the program.
  723.  
  724.    There can be many types of data sections within an executable file;
  725. the three most common are normal data, BSS data and read-only data.
  726. BSS data contains variables and arrays which are to be initialised to
  727. zero at run-time and so is treated as a special case, since the actual
  728. contents of the section need not be stored in the executable file.
  729. Read-only data consists of constant variables and arrays whose contents
  730. are guaranteed not to change when a program is being run.  For example,
  731. on a typical SVR4 UNIX system the following variable definitions would
  732. result in them being placed in the following sections:
  733.  
  734.      int a;           /* BSS data */
  735.      int b = 1;       /* normal data */
  736.      const int c = 2; /* read-only data */
  737.  
  738.    In C the first example would be considered a _tentative_
  739. declaration, and if there was no subsequent definition of that variable
  740. in the current translation unit then it would become a _common_
  741. variable in the resulting object file.  When the object file gets
  742. linked with other object files, any common variables with the same name
  743. become one variable, or take their definition from a non-tentative
  744. definition of that variable.  In the former case, the variable is
  745. placed in the BSS section.  Note that C++ has no support for tentative
  746. declarations.
  747.  
  748.    As all static memory allocations have sizes and address offsets that
  749. are known at compile-time and are explicitly initialised, there is very
  750. little that can go wrong with them.  Data can be read or written past
  751. the end of such variables, but that is a common problem with all memory
  752. allocations and is generally easy to locate in that case.  On systems
  753. that separate read-only data from normal data, writing to a read-only
  754. variable can be quickly diagnosed at run-time.
  755.  
  756.    ---------- Footnotes ----------
  757.  
  758.    (1) Or more accurately, at link time.
  759.  
  760. 
  761. File: mpatrol.info,  Node: Stack memory allocations,  Next: Dynamic memory allocations,  Prev: Static memory allocations,  Up: Memory allocations
  762.  
  763. Stack memory allocations
  764. ========================
  765.  
  766.    The second type of memory allocation is known as a _stack memory
  767. allocation_, which corresponds to non-static local variables and
  768. call-by-value parameter variables.  The sizes of these allocations are
  769. fixed at the time of compilation but their addresses will vary
  770. depending on when the function which defines them is called.  Their
  771. contents are not immediately initialised, and must be explicitly
  772. initialised by the programmer upon entry to the function or when they
  773. become visible in scope.
  774.  
  775.    Such memory allocations are placed in a system memory area called the
  776. _stack_, which is allocated per process(1) and generally grows down in
  777. memory.  When a function is called, the state of the calling function
  778. must be preserved so that when the called function returns, the calling
  779. function can resume execution.  That state is stored on the stack,
  780. including all local variables and parameters.  The compiler generates
  781. code to increase the size of the stack upon entry to a function, and
  782. decrease the size of the stack upon exit from a function, as well as
  783. saving and restoring the values of registers.
  784.  
  785.    There are a few common problems using stack memory allocations, and
  786. most generally involve uninitialised variables, which a good compiler
  787. can usually diagnose at compile-time.  Some compilers also have options
  788. to initialise all local variables with a bit pattern so that
  789. uninitialised stack variables will cause program faults at run-time.
  790. As with static memory allocations, there can be problems with reading
  791. or writing past the end of stack variables, but as their sizes are
  792. fixed these can usually easily be located.
  793.  
  794.    ---------- Footnotes ----------
  795.  
  796.    (1) Or per thread on some systems.
  797.  
  798. 
  799. File: mpatrol.info,  Node: Dynamic memory allocations,  Prev: Stack memory allocations,  Up: Memory allocations
  800.  
  801. Dynamic memory allocations
  802. ==========================
  803.  
  804.    The last type of memory allocation is known as a _dynamic memory
  805. allocation_, which corresponds to memory allocated via `malloc()' or
  806. `operator new[]'.  The sizes, addresses and contents of such memory vary
  807. at run-time and so can cause a lot of problems when trying to diagnose
  808. a fault in a program.  These memory allocations are called dynamic
  809. memory allocations because their location and size can vary throughout
  810. the lifetime of a program.
  811.  
  812.    Such memory allocations are placed in a system memory area called the
  813. _heap_, which is allocated per process on some systems, but on others
  814. may be allocated directly from the system in scattered blocks.  Unlike
  815. memory allocated on the stack, memory allocated on the heap is not
  816. freed when a function or scope is exited and so must be explicitly
  817. freed by the programmer.  The pattern of allocations and deallocations
  818. is not guaranteed to be (and is not really expected to be) linear and
  819. so the functions that allocate memory from the heap must be able to
  820. efficiently reuse freed memory and resize existing allocated memory on
  821. request.  In some programming languages there is support for a _garbage
  822. collector_, which attempts to automatically free memory that has had
  823. all references to it removed, but this has traditionally not been very
  824. popular for programming languages such as C and C++, and has been more
  825. widely used in functional languages like ML(1).
  826.  
  827.    Because dynamic memory allocations are performed at run-time rather
  828. than compile-time, they are outwith the domain of the compiler and must
  829. be implemented in a run-time package, usually as a set of functions
  830. within a linker library.  Such a package manages the heap in such a way
  831. as to abstract its underlying structure from the programmer, providing
  832. a common interface to heap management on different systems.  However,
  833. this _malloc library_ must decide whether to implement a fast memory
  834. allocator, a space-conserving memory allocator, or a bit of both.  It
  835. must also try to keep its own internal tables to a minimum so as to
  836. conserve memory, but this means that it has very little capability to
  837. diagnose errors if any occur.
  838.  
  839.    In some compiler implementations there is a builtin function called
  840. `alloca()'.  This is a dynamic memory allocation function that allocates
  841. memory from the stack rather than the heap, and so the memory is
  842. automatically freed when the function that called it returns.  This is
  843. a non-standard feature that is not guaranteed to be present in a
  844. compiler, and indeed may not be possible to implement on some systems.
  845. However, some compilers now support variable length arrays which
  846. provide roughly the same functionality.
  847.  
  848.    As can be seen from the above paragraphs, dynamic memory allocations
  849. are the types of memory allocations that can cause the most problems in
  850. a program since almost nothing about them can be used by the compiler
  851. to give the programmer useful warnings about using uninitialised
  852. variables, using freed memory, running off the end of a
  853. dynamically-allocated array, etc.  It is these types of memory
  854. allocation problems that the mpatrol library loves to get its teeth
  855. into!
  856.  
  857.    ---------- Footnotes ----------
  858.  
  859.    (1) There is currently at least one garbage collection package
  860. available for C and C++ (*note Related software::).
  861.  
  862. 
  863. File: mpatrol.info,  Node: Operating system support,  Next: Using mpatrol,  Prev: Memory allocations,  Up: Top
  864.  
  865. Operating system support
  866. ************************
  867.  
  868.    Beneath every malloc library's public interface there is the
  869. underlying operating system's memory management interface.  This
  870. provides features which can be as simple as giving processes the
  871. ability to allocate a new block of memory for themselves, or it can
  872. offer advanced features such as protecting areas of memory from being
  873. read or written.  Some embedded systems have no operating systems and
  874. hence no support for dynamic memory allocation, and so the malloc
  875. library must instead allocate blocks of memory from a fixed-sized array.
  876. The mpatrol library can be built to support all of the above types of
  877. system, but the more features an operating system can provide it with,
  878. the more it can do.
  879.  
  880.    On operating systems such as UNIX and Windows, all dynamic memory
  881. allocation requests from a process are dealt with by using a feature
  882. called _virtual memory_.  This means that a process cannot perform
  883. illegal requests without them being denied, which protects the other
  884. running processes and the operating system from being affected by such
  885. errors.  However, on AmigaOS and Netware platforms there is no virtual
  886. memory support and so all processes effectively share the same address
  887. space as the operating system and any other running processes.  This
  888. means that one process can accidentally write into the data structures
  889. of another process, usually causing the other process to fail and bring
  890. down the system.  In addition, a process which allocates a lot of memory
  891. will result in there being less available memory for other running
  892. processes, and in extreme cases the operating system itself.
  893.  
  894. * Menu:
  895.  
  896. * Virtual memory::                  Addition facilities.
  897. * Call stacks and symbol tables::   Tracing information.
  898. * Threads::                         Multiple threads of execution.
  899.  
  900. 
  901. File: mpatrol.info,  Node: Virtual memory,  Next: Call stacks and symbol tables,  Up: Operating system support
  902.  
  903. Virtual memory
  904. ==============
  905.  
  906.    _Virtual memory_ is an operating system feature that was originally
  907. used to provide large usable address spaces for every process on
  908. machines that had very little physical memory.  It is used by an
  909. operating system to fool(1) a running process into believing that it can
  910. allocate a vast amount of memory for its own purposes, although whether
  911. it is allowed to or not depends on the operating system and the
  912. permissions of the individual user.
  913.  
  914.    Virtual memory works by translating a virtual address (which the
  915. process uses) into a physical address (which the operating system
  916. uses).  It is generally implemented via a piece of hardware called a
  917. _memory management unit_, or MMU.  The MMU's primary job is to
  918. translate any virtual addresses that are referred to by machine
  919. instructions into physical addresses by looking up a table which is
  920. built by the operating system.  This table contains mappings to and
  921. from _pages_(2) rather than bytes since it would otherwise be very
  922. inefficient to handle mappings between individual bytes.  As a result,
  923. every virtual memory operation operates on pages, which are indivisible
  924. and are always aligned to the system page size.
  925.  
  926.    Even though each process can now see a huge address space, what
  927. happens when it attempts to allocate more pages than actually
  928. physically exist, or allocate an additional page of memory when all of
  929. the physical pages are in use by it and other processes?  This problem
  930. is solved by the operating system temporarily saving one or more of the
  931. least-used pages (which might not necessarily belong that that process)
  932. to a special place in the file system called a _swap file_, and mapping
  933. the new pages to the physical addresses where the old pages once
  934. resided.  The old pages which have been _swapped out_ are no longer
  935. currently accessible, but their location in the swap file is noted in
  936. the translation table.
  937.  
  938.    However, if one of the pages that has been swapped out is accessed
  939. again, a _page fault_ occurs at the instruction which referred to the
  940. address and the operating system catches this and reloads the page from
  941. the swap file, possibly having to swap out another page to make space
  942. for the new one.  If this occurs too often then the operating system
  943. can slow down, having to constantly swap in and swap out the same pages
  944. over and over again.  Such a problem is called _thrashing_ and can only
  945. really be overcome by using less virtual memory or buying more physical
  946. memory.
  947.  
  948.    It is also possible to take advantage of the virtual memory system's
  949. interaction between physical memory and the file system in program
  950. code, since mapping an existing file to memory means that the usual
  951. file I/O operations can be replaced with memory read and write
  952. operations.  The operating system will work out the optimum way to read
  953. and write any buffers and it means that only one copy of the file
  954. exists in both physical memory and the file system.  Note that this is
  955. how _shared libraries_(3) on UNIX platforms are generally implemented,
  956. with each individual process that uses the shared library having it
  957. mapped to somewhere in its address space.
  958.  
  959.    Another major feature of virtual memory is its ability to read
  960. protect and write protect individual pages of process memory.  This
  961. means that the operating system can control access to different parts
  962. of the address space for each process, and also means that a process
  963. can read and/or write protect an area of memory when it wants to ensure
  964. that it won't ever read or write to it again.  If an illegal memory
  965. access is detected then a _signal_ will be sent to the process, which
  966. can either be caught and handled or will otherwise terminate the
  967. process.  Note that as with all virtual memory operations, this ability
  968. to protect memory only applies to pages, so that it is not possible to
  969. protect individual bytes.
  970.  
  971.    However, some versions of UNIX have programmable software _watch
  972. points_ which are implemented at operating system level.  These are
  973. normally used by debuggers to watch a specified area of memory that is
  974. expected to be read from or written to, but can just as easily be used
  975. to implement memory protection at byte level.  Unfortunately, as this
  976. feature is implemented in software(4) rather than in hardware, watch
  977. points tend to be incredibly slow, mainly as a result of the operating
  978. system having to check every instruction before it is executed.
  979.  
  980.    There is also an additional problem when using watch points, which
  981. is due to misaligned reads from memory.  These can occur with
  982. compiler-generated code or with optimised library routines where memory
  983. read, move or write operations have been optimised to work at word
  984. level rather than byte level.  For example, the `memcpy()' function
  985. would normally be written to copy memory a byte at a time, but on some
  986. systems this can be improved by copying a word at a time.
  987. Unfortunately, care has to be taken when reading and writing such words
  988. as the equivalent bytes may not be aligned on word boundaries.
  989. Technically, reading additional bytes before or after a memory
  990. allocation when they share the same word is legal, but when using watch
  991. points such errors will be picked up.  The mpatrol library replaces
  992. most of the memory operation functions provided by the system libraries
  993. with safer versions, although they may not be as efficient.
  994.  
  995.    An operating system with virtual memory is usually going to run ever
  996. so slightly slower than an operating system without it(5), but the
  997. advantages of virtual memory far outweigh the disadvantages, especially
  998. when used for debugging purposes.
  999.  
  1000.    ---------- Footnotes ----------
  1001.  
  1002.    (1) Well, perhaps that's too harsh a word, but it will certainly seem
  1003. that way to a process running on a 32-bit UNIX system with only 4
  1004. megabytes of physical memory, and yet it will be able to read from and
  1005. write to over 4 gigabytes of virtual memory!
  1006.  
  1007.    (2) The size of a page varies between operating systems and
  1008. processor architectures, but they are generally around 4 or 8 kilobytes
  1009. in size, and are always a power of two.
  1010.  
  1011.    (3) DLLs on Windows platforms.
  1012.  
  1013.    (4) The operating system is still considered software.
  1014.  
  1015.    (5) Due to the overhead of having to translate every address and
  1016. swap in and out pages -- although memory mapped files will usually be
  1017. more efficient than using normal file operations on a system without
  1018. virtual memory.
  1019.  
  1020. 
  1021. File: mpatrol.info,  Node: Call stacks and symbol tables,  Next: Threads,  Prev: Virtual memory,  Up: Operating system support
  1022.  
  1023. Call stacks and symbol tables
  1024. =============================
  1025.  
  1026.    As stated in the section on stack memory allocations (*note Stack
  1027. memory allocations::), when a function is called, a copy of the
  1028. caller's state information (including local variables and registers) is
  1029. saved on the stack so that it can be restored when the called function
  1030. returns.  On many operating systems there is a _calling convention_(1)
  1031. which defines the layout of such stack entries so that code compiled in
  1032. different languages and with different compilers can be intermixed.
  1033. This usually specifies at which stack offsets the stack pointer,
  1034. program counter and local variables for the calling function can be
  1035. found, although on some processor architectures the function calling
  1036. conventions are specified by the hardware and so the operating system
  1037. must use these instead.
  1038.  
  1039.    On systems that have consistent calling conventions, it is usually
  1040. possible to perform call stack _tracebacks_ from within the current
  1041. function in order to determine the stack of function calls that led to
  1042. the current function.  This is extremely useful for debugging purposes
  1043. and is done by examining the current stack frame to see if there is a
  1044. pointer to the previous stack frame.  If there is, then it can be
  1045. followed to find out all of the state information about the calling
  1046. function.  This can be repeated until there are no more stack frames.
  1047. This is generally how this information is determined by debuggers when
  1048. a call stack traceback is requested.
  1049.  
  1050.    In addition to the pointer to the previous stack frame, the saved
  1051. state information also always contains the saved program counter
  1052. register, which contains either the address of the instruction that
  1053. performed the function call, or the address of the instruction at which
  1054. to continue execution when the called function returns(2).  This
  1055. information can be used to identify which function performed the call,
  1056. since the address of the instruction must lie between the start and end
  1057. of one of the functions in the process.
  1058.  
  1059.    However, in order to determine this symbolic information, it must be
  1060. possible to find out where the start and end addresses of all of the
  1061. functions in the process are.  This can usually only be read from
  1062. object files, since they contain the symbol tables that were used by
  1063. the linker to generate the final executable file for the program.  The
  1064. object file's symbol tables normally contain information about the
  1065. start address, size, name and visibility of every symbol that was
  1066. defined, but this depends on the format of the object file and if the
  1067. symbol tables have been stripped from the final executable file.
  1068.  
  1069.    If the object file was created by a compiler then it may also
  1070. contain debugging information that was generated by the compiler for
  1071. use with a debugger.  Such information may include a mapping of code
  1072. addresses to source lines(3), and this information can be used by the
  1073. mpatrol library to provide more meaningful information in call stack
  1074. tracebacks.
  1075.  
  1076.    On systems that support shared libraries, additional work must be
  1077. done to determine the symbolic information for all of the functions
  1078. which have been defined in them.  The symbols for functions that are
  1079. defined in shared libraries normally appear as undefined symbols in the
  1080. executable file for the program and so must be searched in the system
  1081. in order to get the necessary information.  It is usually necessary to
  1082. liaise with the _dynamic linker_(4) on many systems.
  1083.  
  1084.    ---------- Footnotes ----------
  1085.  
  1086.    (1) Usually part of the _Application Binary Interface_, or ABI.
  1087.  
  1088.    (2) Also known as the _return address_.
  1089.  
  1090.    (3) Generally known as a line number table.
  1091.  
  1092.    (4) Which is the part of the operating system that performs the
  1093. run-time linking of shared libraries.
  1094.  
  1095. 
  1096. File: mpatrol.info,  Node: Threads,  Prev: Call stacks and symbol tables,  Up: Operating system support
  1097.  
  1098. Threads
  1099. =======
  1100.  
  1101.    On systems with virtual memory, such as UNIX and Windows, user
  1102. programs are run as _processes_ which have their own address space and
  1103. resources.  If a process needs to create sub-processes to perform other
  1104. tasks it must call `fork()' or `spawn()' to create new processes, but
  1105. these new processes do not share the same address space or resources as
  1106. the parent process.  If processes need to share memory they must either
  1107. use a message passing interface or explicitly mark a range of memory as
  1108. shareable.
  1109.  
  1110.    Traditionally, this was not too much of a handicap as parallel
  1111. processing was an expensive luxury and could only be made use of by the
  1112. kernel of such systems.  However, with the birth of fast processors and
  1113. parallel programming, programs could be made to run more efficiently
  1114. and faster on multi-processor systems by having more than one _thread_
  1115. of control.  This was achieved by allowing processes to have more than
  1116. one program counter through which the processor could execute
  1117. instructions, and if one thread of control stalled for a particular
  1118. reason then another could continue without stalling the entire process.
  1119.  
  1120.    Such multithreaded programs allow parallel programming and implicit
  1121. shared memory between threads since all threads in a process share the
  1122. same address space and resources.  This is similar to operating systems
  1123. that have no virtual memory, such as AmigaOS and Netware(1), except
  1124. that once a process terminates, all threads terminate as well and all
  1125. of its resources are still reclaimed.
  1126.  
  1127.    Multithreaded programming generally needs no compiler support, but
  1128. does require some primitive operations to be supported by the operating
  1129. system for a threads library to call.  The functions that are available
  1130. in the threads library provide the means for a process to create and
  1131. destroy threads.  There are currently several popular threads libraries
  1132. available, although the POSIX threads standard remains the definitive
  1133. implementation.
  1134.  
  1135.    It is always important to remember when programming a multithreaded
  1136. application that because all threads in a process share the same
  1137. address space, measures must be taken to prevent threads reading and
  1138. writing global data in a haphazard fashion.  This can either be done by
  1139. locking with semaphores and mutexes, or can be performed by using stack
  1140. variables instead of global variables since every thread has its own
  1141. local stack.  Care must be taken to write re-entrant functions -- i.e.
  1142. a function will give exactly the same result with one thread as it will
  1143. with multiple threads running it at the same time.
  1144.  
  1145.    ---------- Footnotes ----------
  1146.  
  1147.    (1) Where the kernel is effectively a single process running all
  1148. user programs as threads.
  1149.  
  1150. 
  1151. File: mpatrol.info,  Node: Using mpatrol,  Next: Profiling,  Prev: Operating system support,  Up: Top
  1152.  
  1153. Using mpatrol
  1154. *************
  1155.  
  1156.    This chapter contains a general description of all of the features
  1157. of mpatrol and how to use them effectively.  You'll also find a
  1158. complete reference for mpatrol in the appendices, but you may wish to
  1159. try out the examples (*note Examples::) and the tutorial (*note
  1160. Tutorial::) before reading further.
  1161.  
  1162. * Menu:
  1163.  
  1164. * Library behaviour::           Miscellaneous information.
  1165. * Logging and tracing::         General information.
  1166. * General errors::              Everyday problems.
  1167. * Overwrites and underwrites::  Overflow buffers.
  1168. * Using with a debugger::       Setting breakpoints.
  1169. * Testing::                     Additional checks.
  1170. * Library functions::           What's provided.
  1171. * Utilities::                   External programs.
  1172.  
  1173. 
  1174. File: mpatrol.info,  Node: Library behaviour,  Next: Logging and tracing,  Up: Using mpatrol
  1175.  
  1176. Library behaviour
  1177. =================
  1178.  
  1179.    Most of the behaviour of the mpatrol library can be controlled at
  1180. run-time via options which are read from the `MPATROL_OPTIONS'
  1181. environment variable.  This prevents you having to recompile or relink
  1182. each time you want to change a library setting, and so makes it really
  1183. easy to try out different settings to locate a particular bug.  You
  1184. should know how to set the value of an environment variable on your
  1185. system before you read on.
  1186.  
  1187.    By default, the mpatrol library will attempt to determine the
  1188. minimum required alignment for any generic memory allocation when it
  1189. first initialises itself.  This may be affected by the compiler and its
  1190. settings when the library was built but it should normally reflect the
  1191. minimum alignment required by the processor on your system.  If you
  1192. would prefer a larger (or perhaps even smaller) default alignment you
  1193. may change it at run-time using the `DEFALIGN' option.  The value you
  1194. supply must be in bytes, must be a power of two, and should not be
  1195. larger that the system page size.  If you encounter bus errors due to
  1196. misaligned memory accesses then you should increase this value.
  1197.  
  1198.    On systems that have virtual memory the library will attempt to
  1199. write-protect all of its internal structures when user code is being
  1200. run.  This ensures that it is nearly impossible for a program to
  1201. corrupt any mpatrol library data.  However, unprotecting and then
  1202. protecting the structures at every library call has a slight overhead
  1203. so you may prefer to disable this behaviour by using the `NOPROTECT'
  1204. option.  This has no effect on systems that have no virtual memory.
  1205.  
  1206.    Usually it is desirable for many system library routines to be
  1207. protected from being interrupted by certain signals since they may
  1208. themselves be called from signal handlers.  If this is not the case
  1209. then it may be possible to interrupt the program from within such
  1210. routines, perhaps causing problems if their global variables are left
  1211. in an undefined state.  As the mpatrol library replaces some of these
  1212. system library routines it is also possible to specify that they are
  1213. protected from certain interrupt signals using the `SAFESIGNALS' option.
  1214. However, this can sometimes result in it being hard to interrupt the
  1215. program from the keyboard if a lot of processor time is spent in
  1216. mpatrol routines, which is why this behaviour is disabled by default(1).
  1217.  
  1218.    On UNIX systems, the usual way for malloc libraries to allocate
  1219. memory from the process heap is through the `sbrk()' system call.  This
  1220. allocates memory from a contiguous heap, but has the disadvantage in
  1221. that other library functions may also allocate memory using the same
  1222. function, thus creating holes in the heap.  This is not a problem for
  1223. mpatrol, but you may have a suspicion that your bug is due to a
  1224. function from another library corrupting your data so you may wish to
  1225. use the `USEMMAP' option.  This is only available on systems that have
  1226. the `mmap()' system call and allows mpatrol to allocate all of its
  1227. memory from a part of the process heap that is non-contiguous (i.e.
  1228. each call to `mmap()' may return a block of memory that is completely
  1229. unrelated to that returned by the previous call).
  1230.  
  1231.    By default, every time an mpatrol library function is called the
  1232. library will automatically check the freed memory and overflow buffers
  1233. of every memory allocation, which can slow program execution down,
  1234. especially if you suspect the error you are looking for occurs at the
  1235. 1000th memory allocation, for example.  You can therefore use the
  1236. `CHECK' option to specify a range of memory allocations at which the
  1237. mpatrol library will automatically check the freed memory and overflow
  1238. buffers.  All other allocations that fall outside this range will not
  1239. be checked.
  1240.  
  1241.    If the mpatrol library that was built for your system supports
  1242. reading symbolic information from a program's executable file, but it
  1243. cannot locate the executable file, or you wish to specify an
  1244. alternative, you can use the `PROGFILE' option to do this.  All this
  1245. does is instruct the mpatrol library to read symbols from this file
  1246. instead.  Note that on systems that support dynamic linking, the
  1247. library can also read symbols from a dynamically linked executable file
  1248. that has had its normal symbol table stripped.
  1249.  
  1250.    Finally, a list of all of the recognised options in the mpatrol
  1251. library can be displayed to the standard error file stream by using the
  1252. `HELP' option.  This will not affect the settings of the library in any
  1253. way, so you should be able to use other options at the same time.
  1254.  
  1255.    ---------- Footnotes ----------
  1256.  
  1257.    (1) In mpatrol release 1.0 it was enabled by default.
  1258.  
  1259. 
  1260. File: mpatrol.info,  Node: Logging and tracing,  Next: General errors,  Prev: Library behaviour,  Up: Using mpatrol
  1261.  
  1262. Logging and tracing
  1263. ===================
  1264.  
  1265.    If you would like to see a complete log of all of the memory
  1266. allocations, reallocations and deallocations performed by your program,
  1267. use the `LOGALL' option.  This provides detailed tracing for each of
  1268. the mpatrol library functions, and a full description of the format of
  1269. such tracing is given in Example 1 (*note Example 1::).  Alternatively,
  1270. you may select one or more types of functions to be traced using the
  1271. `LOGALLOCS', `LOGREALLOCS', `LOGFREES' and `LOGMEMORY' options if you
  1272. feel that the log file is too large when `LOGALL' is used.  By default
  1273. all diagnostics from the mpatrol library get sent to `mpatrol.log' in
  1274. the current directory, but this can be changed using the `LOGFILE'
  1275. option.
  1276.  
  1277.    On systems that support it, every log entry also contains a call
  1278. stack traceback that may also include the names of the symbols that
  1279. appear on the call stack.  If the object file access library that
  1280. mpatrol was built with has support for reading line number tables from
  1281. object files then the `USEDEBUG' option will also try to determine the
  1282. file name and line number for each entry in the call stack, but only if
  1283. the object files contain the relevant debugging information.  This
  1284. information will only be available before program termination and so
  1285. any call stack tracebacks that appear after the library summary will
  1286. not be displayed with their corresponding file name and line number.
  1287. This option will also slow down program execution since a search
  1288. through the line number tables will have to be made every time a call
  1289. stack is displayed.
  1290.  
  1291.    The mpatrol library will always try to display as much useful
  1292. information as possible in this log file, and will always display a
  1293. summary of library settings and statistics when your program terminates
  1294. successfully.  If you don't get this then your program did not call
  1295. `exit()' and either called `abort()' or was terminated by the operating
  1296. system instead.  In such cases, either use a debugger to see where your
  1297. program crashed or use the `LOGALL' option to see the last successful
  1298. library call in the log file so that you have a rough idea of where
  1299. your program crashed.
  1300.  
  1301.    It is also possible to get mpatrol to write more summary information
  1302. to the log file after it writes out its settings and statistics at
  1303. program termination.  Use the `SHOWFREED' and `SHOWUNFREED' options to
  1304. display a list of freed and unfreed memory allocations.  The former
  1305. will only be displayed if the `NOFREE' option is used, but the latter
  1306. can be useful for detecting memory leaks.  The `SHOWMAP' option will
  1307. display a memory map of the heap that was valid when the process
  1308. terminated, and the `SHOWSYMBOLS' option will display any symbolic
  1309. information that the mpatrol library managed to obtain from any
  1310. executable files and libraries that were relevant to the program being
  1311. tested.  All of these options can be selected with the `SHOWALL' option.
  1312.  
  1313. 
  1314. File: mpatrol.info,  Node: General errors,  Next: Overwrites and underwrites,  Prev: Logging and tracing,  Up: Using mpatrol
  1315.  
  1316. General errors
  1317. ==============
  1318.  
  1319.    By default, the mpatrol library follows the guidelines for ANSI C
  1320. regarding the behaviour of the dynamic memory allocation functions it
  1321. replaces(1).  This means that calling `malloc()' with a size of zero is
  1322. allowed, for example.  However, warnings can be generated for all of
  1323. these types of calls by using the `CHECKALL' option.  The `CHECKALLOCS'
  1324. option warns only about calls to `malloc()' and similar functions with
  1325. a size of zero, the `CHECKREALLOCS' option warns only about calls to
  1326. `realloc()' and similar functions with either a null pointer or a size
  1327. of zero, and the `CHECKFREES' option warns only about calls to `free()'
  1328. and similar functions with a null pointer.
  1329.  
  1330.    All newly-allocated memory can be pre-filled with a specified byte
  1331. by using the `ALLOCBYTE' option.  This can be used to catch out code
  1332. that expects newly-allocated memory to be zeroed, although this option
  1333. will have no effect on memory that was allocated with `calloc()'.  All
  1334. free memory can also be pre-filled with a different specified byte by
  1335. using the `FREEBYTE' option.  This will catch out code that expects to
  1336. be able to use the contents of freed memory.
  1337.  
  1338.    Alternatively, the mpatrol library can be instructed to keep all
  1339. freed memory allocations so that its diagnostics can be clearer about
  1340. which freed allocation a piece of code is erroneously trying to access.
  1341. This is controlled with the `NOFREE' option, but since it never reuses
  1342. any freed allocations it can result in a lot more heap memory being
  1343. used.  Note that this option distinguishes between _free_ memory and
  1344. _freed_ memory.  _Free_ memory is unallocated memory that has been
  1345. taken from the system heap.  _Freed_ memory is a freed memory
  1346. allocation, with all of the original details of the allocation
  1347. preserved.
  1348.  
  1349.    Normally, the `NOFREE' option will fill the freed allocation with the
  1350. free byte so that any code that accesses it will hopefully fall over.
  1351. However, the original contents can be preserved using the `PRESERVE'
  1352. option in case you need to see what the contents were just before it
  1353. was freed.  The `NOFREE' option is also affected by the `PAGEALLOC'
  1354. option, since then the freed allocation will have its contents both
  1355. read and write protected so that nothing can access them.  If the
  1356. `PRESERVE' option is used in this case then the freed allocation will
  1357. only be made write-protected so that the original contents can be read
  1358. from but not written to.
  1359.  
  1360.    ---------- Footnotes ----------
  1361.  
  1362.    (1) I attempted to do the same for ANSI C++ but there are still
  1363. namespace and exception handling issues to be resolved.
  1364.  
  1365. 
  1366. File: mpatrol.info,  Node: Overwrites and underwrites,  Next: Using with a debugger,  Prev: General errors,  Up: Using mpatrol
  1367.  
  1368. Overwrites and underwrites
  1369. ==========================
  1370.  
  1371.    Once a block of memory has been allocated, it is imperative that the
  1372. program does not attempt to write any data past the end of the block or
  1373. write any data just before the beginning of the block.  Even writing a
  1374. single byte just beyond the end of an allocation or just before the
  1375. beginning of an allocation can cause havoc.  This is because most
  1376. malloc libraries store the details of the allocated block in the first
  1377. few words before the beginning of the block, such as its size and a
  1378. pointer to the next block.  The mpatrol library does not do this, so a
  1379. program which failed using the normal malloc library and worked when
  1380. the mpatrol library was linked in is a possible candidate for turning
  1381. on overflow buffers.
  1382.  
  1383.    Such memory corruption can be extremely difficult to pinpoint as it
  1384. is unlikely to show itself until the next call is made to the malloc
  1385. library, or if the internal malloc library blocks were not overwritten,
  1386. the next time the data is read from the block that was overwritten.  If
  1387. the former is the case then the next library call will cause an
  1388. internal error or a crash, but only when the memory block that was
  1389. affected is referenced.  This is likely to disappear when using the
  1390. mpatrol library since it keeps its internal structures separate, and
  1391. write-protects them on systems that support memory protection.
  1392.  
  1393.    In order to identify such errors, it is possible to place special
  1394. buffers(1) on either side of every memory allocation, and these will be
  1395. pre-filled with a specified byte.  Before every mpatrol library call,
  1396. the library will check the integrity of every such overflow buffer in
  1397. order to check for a memory underwrite or overwrite.  Depending on the
  1398. number of allocations and size of these buffers, this can take a
  1399. noticable amount of time (which is why overflow buffers are disabled by
  1400. default), but can mean that these errors get noticed sooner.  The
  1401. option which governs this is `OFLOWSIZE'.  The byte with which they get
  1402. pre-filled can be changed with `OFLOWBYTE'.  Depending on what gets
  1403. written, it might only be possible to see such errors when a different
  1404. size of buffer or a different pre-fill byte is used.
  1405.  
  1406.    A worse situation can occur when it is only reads from memory that
  1407. overflow or underflow; i.e. with the faulty code reading just before or
  1408. just past a memory allocation.  These cannot be detected by overflow
  1409. buffers as it is not possible using conventional means to interrupt
  1410. every single read from memory.  However, on systems with virtual
  1411. memory, it is possible to use the memory protection feature to provide
  1412. an alternative to overflow buffers, although at the added expense of
  1413. increased memory usage.
  1414.  
  1415.    The `PAGEALLOC' option turns on this feature and automatically rounds
  1416. up the size of every memory allocation to a multiple of the system page
  1417. size.  It also rounds up the size of every overflow buffer to a
  1418. multiple of the system page size so that every memory allocation
  1419. occupies its own set of pages of virtual memory and no two memory
  1420. allocations occupy the same page of virtual memory.  The overflow
  1421. buffers are then read and write protected so that any memory accesses
  1422. to them will generate an error(2).  Following on from the previous
  1423. section, the `PAGEALLOC' option also causes free memory to be read and
  1424. write protected as well since that will also occupy non-overlapping
  1425. virtual memory pages.
  1426.  
  1427.    The remaining memory that is left over within an allocation's pages
  1428. is effectively turned into traditional overflow buffers, being
  1429. pre-filled with the overflow byte and checked periodically by the
  1430. mpatrol library to ensure that nothing has written into them.  However,
  1431. because of this remaining memory, the library has a choice of where to
  1432. place the memory allocation within its pages.  If it places the
  1433. allocation at the very beginning then it will catch memory underwrites,
  1434. but if it places the allocation at the very end then it will catch
  1435. memory overwrites.  Such a choice can be controlled at run-time by
  1436. supplying an argument to the `PAGEALLOC' option.  If `PAGEALLOC=LOWER'
  1437. is used then every allocation will be placed at the very beginning of
  1438. its pages and if `PAGEALLOC=UPPER' is used then the placement will be
  1439. at the very end of its pages.  This is probably better explained in
  1440. Example 3 (*note Example 3::) where the problems with `PAGEALLOC=UPPER'
  1441. and alignment are also discussed.
  1442.  
  1443.    Obviously, there are still some deficiencies when using `PAGEALLOC'
  1444. since it can use up a huge amount of memory (especially with `NOFREE')
  1445. and the overflow buffers within an allocation's pages can still be read
  1446. without causing an immediate error.  Both of these deficiencies can be
  1447. overcome by using the `OFLOWWATCH' option to install _software watch
  1448. points_ instead of overflow buffers, but there are still very few
  1449. systems that support software watch points at the moment, and it can
  1450. slow a program's execution speed down by a factor of around 10,000.
  1451. The reason for this is that software watch points instruct the
  1452. operating system to check every read from and write to memory, which
  1453. means that it has to single-step through a process checking every
  1454. instruction before it is executed.  However, this is a very thorough
  1455. way of checking for overflows and is unlikely to miss anything,
  1456. although there may be problems with misaligned memory accesses when
  1457. using watch points (*note Virtual memory::).
  1458.  
  1459.    Note that from release 1.1.0 of mpatrol, the library comes with
  1460. replacement functions for many memory operation functions, such as
  1461. `memset()' and `memcpy()'.  These new functions provide additional
  1462. checks to ensure that if a memory operation is being performed on a
  1463. memory block, the operation will not read or write before or beyond the
  1464. boundaries of that block.
  1465.  
  1466.    Normally, if an error is discovered in the call to such functions,
  1467. the mpatrol library will report the error but prevent the operation
  1468. from being performed before continuing execution.  If the error was
  1469. that the range of memory being operated on overflowed the boundaries of
  1470. an existing memory allocation then the `ALLOWOFLOW' option can be used
  1471. to turn the error into a warning and force the operation to continue.
  1472. This behaviour can be desirable in certain cases where third-party
  1473. libraries are being used that make such calls but the end result does
  1474. not overflow the allocation boundary.
  1475.  
  1476.    To conclude, if you suspect your program has a piece of code which
  1477. is performing illegal memory underwrites or overwrites to a memory
  1478. allocation you should use each of the following options in sequence,
  1479. but only if your system supports them.
  1480.  
  1481.   1. `OFLOWSIZE=8'
  1482.  
  1483.   2. `OFLOWSIZE=32'
  1484.  
  1485.   3. `OFLOWSIZE=1' `PAGEALLOC=LOWER'
  1486.  
  1487.   4. `OFLOWSIZE=1' `PAGEALLOC=UPPER'
  1488.  
  1489.   5. `OFLOWSIZE=8' `OFLOWWATCH'
  1490.  
  1491.   6. `OFLOWSIZE=32' `OFLOWWATCH'
  1492.  
  1493.    ---------- Footnotes ----------
  1494.  
  1495.    (1) Commonly known as _overflow buffers_ or _fence posts_.
  1496.  
  1497.    (2) This is a feature that was first used by Electric Fence (*note
  1498. Related software::) to track down memory corruption.
  1499.  
  1500. 
  1501. File: mpatrol.info,  Node: Using with a debugger,  Next: Testing,  Prev: Overwrites and underwrites,  Up: Using mpatrol
  1502.  
  1503. Using with a debugger
  1504. =====================
  1505.  
  1506.    If you would like to use mpatrol to pause at a specific memory
  1507. allocation, reallocation or deallocation in a debugger then this
  1508. section will describe how to go about it.  Unfortunately, debuggers
  1509. vary widely in function and usage and are normally very
  1510. system-dependent.  The example below will use `gdb' as the debugger,
  1511. but as long as you know how to set a breakpoint within a debugger, any
  1512. one will do.
  1513.  
  1514.    First of all, decide where you would like the mpatrol library to
  1515. pause when running your program within the debugger.  You can choose
  1516. one allocation index to break at using the `ALLOCSTOP' option, or you
  1517. can choose to break at a specific reallocation of that allocation by
  1518. also using the `REALLOCSTOP' option.  If you use `REALLOCSTOP' without
  1519. using `ALLOCSTOP' then you will break at the first memory allocation
  1520. that has been reallocated the specified number of times.  You can also
  1521. choose to break at the point in your program that frees a specific
  1522. allocation index by using the `FREESTOP' option.
  1523.  
  1524.    The normal process for determining where you would like to pause
  1525. your program in the debugger is by using the `LOGALL' option and
  1526. examining the log file produced by mpatrol.  If your program crashed
  1527. then you should look at the last entry in the log file to see what the
  1528. allocation index (and possibly also the reallocation index) of the last
  1529. successful call was.  You can then decide which of the above options to
  1530. use.  Note that the debugger will break at a point before any work is
  1531. done by the mpatrol library for that allocation index so that you can
  1532. see if it was the last successful operation that caused the damage.
  1533.  
  1534.    Having decided which combination of mpatrol options to use, you
  1535. should set them in the `MPATROL_OPTIONS' environment variable before
  1536. running the debugger on your program.  Alternatively, your debugger may
  1537. have a command that allows you to modify your environment during
  1538. debugging, but you're just as well setting the environment variable
  1539. before you run the debugger as it shouldn't make any difference(1).
  1540.  
  1541.    After you get to the debugger command prompt, you should set a
  1542. breakpoint at the `__mp_trap()' function.  This is the function that
  1543. gets called when the specified allocation index and/or reallocation
  1544. index appears and so when you run your program under the debugger the
  1545. mpatrol library will call `__mp_trap()' and the debugger will stop at
  1546. that point.  If you are not running your program within a debugger, or
  1547. if you haven't set the breakpoint, then `__mp_trap()' will still be
  1548. called, but it won't do anything.  Note that there may be some naming
  1549. issues on some platforms where the visible name of a global function
  1550. gets an underscore prepended to it.  You may have to take that into
  1551. account when setting the breakpoint on such systems.
  1552.  
  1553.    Now that you have set the `MPATROL_OPTIONS' environment variable and
  1554. have set the debugger to break at `__mp_trap()', all that is required
  1555. is for you to run your program.  Hopefully, the debugger should stop at
  1556. `__mp_trap()'.  If it doesn't then you may have to check your
  1557. environment variable settings to ensure that they are the same as when
  1558. you ran the program outwith the debugger, although obviously with the
  1559. addition of `ALLOCSTOP', etc.  Once the program has been halted by the
  1560. debugger, you can then single-step through your code until you see
  1561. where it goes wrong.  If this is near the end of your program then
  1562. you'll have saved yourself a lot of time by using this method.
  1563.  
  1564.    The following example will be used to illustrate the steps involved
  1565. in using the `ALLOCSTOP', `REALLOCSTOP' and `FREESTOP' options.
  1566. However, it is only for tutorial purposes and the same effect could
  1567. easily be achieved by breaking at line 18 in a debugger because in this
  1568. case it is obvious from the code and the mpatrol log file where it is
  1569. going wrong.  In real programs this is hardly ever the case(2).
  1570.  
  1571.       1  /*
  1572.       2   * Allocates 1000 blocks of 16 bytes, freeing each block immediately
  1573.       3   * after it is allocated, and freeing the last block twice.
  1574.       4   */
  1575.      
  1576.      
  1577.       7  #include "mpatrol.h"
  1578.      
  1579.      
  1580.      10  int main(void)
  1581.      11  {
  1582.      12      void *p;
  1583.      13      int i;
  1584.      
  1585.      15      for (i = 0; i < 1000; i++)
  1586.      16          if (p = malloc(16))
  1587.      17              free(p);
  1588.      18      free(p);
  1589.      19      return EXIT_SUCCESS;
  1590.      20  }
  1591.  
  1592.    Compile this example code with debugging information enabled and
  1593. link it with the mpatrol library, then set `MPATROL_OPTIONS' to
  1594. `LOGALL' and run the resulting program.  If you examine `mpatrol.log'
  1595. you will see the following near the bottom of the file.
  1596.  
  1597.      ...
  1598.      
  1599.      ALLOC: malloc (1000, 16 bytes, 2 bytes) [main|test.c|16]
  1600.              0x80000D8E main
  1601.              0x80000D24 _start
  1602.      
  1603.      returns 0x80033000
  1604.      
  1605.      FREE: free (0x80033000) [main|test.c|17]
  1606.              0x80000DBE main
  1607.              0x80000D24 _start
  1608.      
  1609.          0x80033000 (16 bytes) {malloc:1000:0} [main|test.c|16]
  1610.              0x80000D8E main
  1611.              0x80000D24 _start
  1612.      
  1613.      FREE: free (0x80033000) [main|test.c|18]
  1614.              0x80000DE8 main
  1615.              0x80000D24 _start
  1616.      
  1617.      ERROR: free: 0x80033000 has not been allocated
  1618.      
  1619.      ...
  1620.  
  1621.    In this example, we'll want to use `ALLOCSTOP' to halt the program at
  1622. the 1000th memory allocation so that we can step through it with a
  1623. debugger.  So, set `MPATROL_OPTIONS' to `ALLOCSTOP=1000' and load the
  1624. program into the debugger.  If you are using `gdb' you can now do the
  1625. following steps, but if you are not you will have to use the equivalent
  1626. commands in your debugger.  Note that `(gdb)' is the debugger command
  1627. prompt and so anything that appears on that line after that should be
  1628. typed as a command.
  1629.  
  1630.      (gdb) break __mp_trap
  1631.      Breakpoint 1 at 0x80004026
  1632.      (gdb) run
  1633.      Starting program: a.out
  1634.      Breakpoint 1, 0x80004026 in __mp_trap()
  1635.      (gdb) backtrace
  1636.      #0  0x80004026 in __mp_trap()
  1637.      #1  0x800027ec in __mp_getmemory()
  1638.      #2  0x80001138 in __mp_alloc()
  1639.      #3  0x80000d8e in main() at test.c:16
  1640.      (gdb) finish
  1641.      Run till exit from #0  0x80004026 in __mp_trap()
  1642.      0x800027ec in __mp_getmemory()
  1643.      (gdb) finish
  1644.      Run till exit from #0  0x800027ec in __mp_getmemory()
  1645.      0x80001138 in __mp_alloc()
  1646.      (gdb) finish
  1647.      Run till exit from #0  0x80001138 in __mp_alloc()
  1648.      0x80000d8e in main() at test.c:16
  1649.      16              if (p = malloc(16))
  1650.      (gdb) step
  1651.      17                  free(p);
  1652.      (gdb) step
  1653.      15          for (i = 0; i < 1000; i++)
  1654.      (gdb) step
  1655.      18          free(p);
  1656.      (gdb) quit
  1657.      The program is running.  Quit anyway (and kill it)? (y or n) y
  1658.  
  1659.    After setting the breakpoint and running the program, the debugger
  1660. halts at `__mp_trap()'.  Because `__mp_trap()' is a function within the
  1661. mpatrol library, you don't want to bother stepping through any of the
  1662. library functions, and in this case you can't since the mpatrol library
  1663. was not compiled with debugging information enabled.  So, after
  1664. returning from all of the library functions, the source line becomes
  1665. line 16 because that was the location of the 1000th memory allocation.
  1666. Single-stepping twice gets us to line 18 which is our destination.
  1667.  
  1668.    Sometimes it is useful to be able to see information about a memory
  1669. allocation whilst running a program from within a debugger.  The
  1670. `__mp_printinfo()' function is provided for that purpose and takes a
  1671. heap address as its only argument.  Using the above example, it would
  1672. have been possible to print out information about the pointer `p' at
  1673. line 17 from within `gdb':
  1674.  
  1675.      (gdb) call __mp_printinfo(p)
  1676.      address 0x80033000 located in allocated block:
  1677.          start of block:     0x80033000
  1678.          size of block:      2 bytes
  1679.          allocated by:       malloc
  1680.          allocation index:   1000
  1681.          reallocation index: 0
  1682.          calling function:   main
  1683.          called from file:   test.c
  1684.          called at line:     16
  1685.          function call stack:
  1686.              0x80000D8E main
  1687.              0x80000D24 _start
  1688.  
  1689.    Some debuggers, such as `gdb', also allow you to define your own
  1690. commands for use in a debugging session.  The following example defines
  1691. a new `gdb' command called `printalloc' which calls
  1692. `__mp_printinfo()'(3):
  1693.  
  1694.      (gdb) define printalloc
  1695.      Type commands for definition of "printalloc".
  1696.      End with a line saying just "end".
  1697.      >call __mp_printinfo($arg0)
  1698.      >end
  1699.      (gdb) document printalloc
  1700.      Type documentation for "printalloc".
  1701.      End with a line saying just "end".
  1702.      >Displays information about an address in the heap.
  1703.      >end
  1704.  
  1705.    ---------- Footnotes ----------
  1706.  
  1707.    (1) Unless you've linked the debugger with the mpatrol library.
  1708.  
  1709.    (2) The other reason that this program is simple is because a proper
  1710. example would generally involve crashing the program, but on AmigaOS
  1711. and Netware that would also involve crashing the system -- not
  1712. something you'd want to do whilst trying this out.
  1713.  
  1714.    (3) A sample GDB command file for use with mpatrol can be found in
  1715. `extra/.gdbinit'.
  1716.  
  1717. 
  1718. File: mpatrol.info,  Node: Testing,  Next: Library functions,  Prev: Using with a debugger,  Up: Using mpatrol
  1719.  
  1720. Testing
  1721. =======
  1722.  
  1723.    The mpatrol library has several features that make it useful when
  1724. testing a program's dynamic memory allocations.  These are features
  1725. that do not help in fixing an existing bug, but rather help to identify
  1726. additional bugs that may be lurking in your code.
  1727.  
  1728.    It is possible to set a simulated upper limit on the amount of heap
  1729. memory available to a process with the `LIMIT' option, which accepts a
  1730. size in bytes, but will be disabled when it is zero.  This can be
  1731. extremely useful for testing a program under simulated low memory
  1732. conditions to see how it handles such errors.  Of course, you should
  1733. set the heap limit to a value less than the amount of actual available
  1734. memory otherwise this option will have no effect.  Note that the
  1735. mpatrol library may use up a small amount of heap memory when it
  1736. initialises itself(1) so the value passed to the `LIMIT' option may
  1737. need to be set slightly higher than you would normally expect.
  1738.  
  1739.    It is also possible to instruct the mpatrol library to randomly fail
  1740. a certain number of memory allocations so that you can further test
  1741. error handling code in a program.  The frequency at which failures
  1742. occur can be controlled with the `FAILFREQ' option, where a value of
  1743. zero means that no failures will occur, but any other value will
  1744. randomly cause failures.  For example, a value of `10' will cause
  1745. roughly one in ten failures and a value of `1' will cause every memory
  1746. allocation to fail.  The random sequence can be made predictable by
  1747. using the `FAILSEED' option.  If this is non-zero then the same program
  1748. run with the same failure frequency and same failure seed will fail on
  1749. exactly the same memory allocations.  If this is zero then the failure
  1750. seed will itself be set randomly, but you can see its value when the
  1751. summary is displayed at program termination.
  1752.  
  1753.    When running _batch tests_(2) it is sometimes useful to be able to
  1754. detect if there have been any memory leaks.  Such leaks should normally
  1755. be distinguished from code which has purposely not freed the memory
  1756. that it allocated, so there may be a certain expected number of unfreed
  1757. allocations at program termination.  It may be that you would like to
  1758. highlight any additional unfreed allocations since they may be due to
  1759. real memory leaks, so the `UNFREEDABORT' option can be set to a
  1760. threshold number of expected unfreed allocations.  If the library
  1761. detects a number of unfreed allocations higher than this then it will
  1762. abort the program at termination so that it fails.  All tests that fail
  1763. in this way can then be examined after the test suite finishes.
  1764.  
  1765.    ---------- Footnotes ----------
  1766.  
  1767.    (1) Actually, it's not really the mpatrol library that uses the
  1768. memory but the object file access libraries since they call `malloc()'
  1769. to allocate any memory that they require.
  1770.  
  1771.    (2) A set of tests that run without user intervention.
  1772.  
  1773. 
  1774. File: mpatrol.info,  Node: Library functions,  Next: Utilities,  Prev: Testing,  Up: Using mpatrol
  1775.  
  1776. Library functions
  1777. =================
  1778.  
  1779.    Along with the standard set of C and C++ dynamic memory allocation
  1780. functions, the mpatrol library also comes with an additional set of
  1781. functions which can be used to provide additional information to your
  1782. program, and which can be called at various points in your code for
  1783. debugging purposes.  You must always include the `mpatrol.h' header
  1784. file in order to use these functions, but you can check for a specific
  1785. version of the mpatrol library by checking the `MPATROL_VERSION'
  1786. preprocessor macro.
  1787.  
  1788.    It is possible to obtain a great deal of information about an
  1789. existing memory allocation using the `__mp_info()' function.  This
  1790. takes an address as an argument and fills in any details about its
  1791. corresponding memory allocation in a supplied structure.  The following
  1792. example illustrates this (it can be found in `tests/pass/test4.c').
  1793.  
  1794.      23  /*
  1795.      24   * Demonstrates and tests the facility for obtaining information
  1796.      25   * about the allocation a specific address belongs to.
  1797.      26   */
  1798.      
  1799.      
  1800.      29  #include "mpatrol.h"
  1801.      30  #include <stdio.h>
  1802.      
  1803.      
  1804.      33  void display(void *p)
  1805.      34  {
  1806.      35      __mp_allocstack *s;
  1807.      36      __mp_allocinfo d;
  1808.      
  1809.      38      if (!__mp_info(p, &d))
  1810.      39      {
  1811.      40          fprintf(stderr, "nothing known about address 0x%08lX\n", p);
  1812.      41          return;
  1813.      42      }
  1814.      43      fprintf(stderr, "block:   0x%08lX\n", d.block);
  1815.      44      fprintf(stderr, "size:    %lu\n", d.size);
  1816.      45      fprintf(stderr, "type:    %lu\n", d.type);
  1817.      46      fprintf(stderr, "alloc:   %lu\n", d.alloc);
  1818.      47      fprintf(stderr, "realloc: %lu\n", d.realloc);
  1819.      48      fprintf(stderr, "func:    %s\n", d.func ? d.func : "NULL");
  1820.      49      fprintf(stderr, "file:    %s\n", d.file ? d.file : "NULL");
  1821.      50      fprintf(stderr, "line:    %lu\n", d.line);
  1822.      51      for (s = d.stack; s != NULL; s = s->next)
  1823.      52      {
  1824.      53          fprintf(stderr, "\t0x%08lX: ", s->addr);
  1825.      54          fprintf(stderr, "%s\n", s->name ? s->name : "NULL");
  1826.      55      }
  1827.      56      fprintf(stderr, "freed:   %d\n", d.freed);
  1828.      57  }
  1829.      
  1830.      
  1831.      60  void func2(void)
  1832.      61  {
  1833.      62      void *p;
  1834.      
  1835.      64      if (p = malloc(16))
  1836.      65      {
  1837.      66          display(p);
  1838.      67          free(p);
  1839.      68      }
  1840.      69      display(p);
  1841.      70  }
  1842.      
  1843.      
  1844.      73  void func1(void)
  1845.      74  {
  1846.      75      func2();
  1847.      76  }
  1848.      
  1849.      
  1850.      79  int main(void)
  1851.      80  {
  1852.      81      func1();
  1853.      82      return EXIT_SUCCESS;
  1854.      83  }
  1855.  
  1856.    When this is compiled and run, it should give the following output,
  1857. although the pointers are likely to be different.
  1858.  
  1859.      block:   0x8000A068
  1860.      size:    16
  1861.      type:    0
  1862.      alloc:   10
  1863.      realloc: 0
  1864.      func:    func2
  1865.      file:    test4.c
  1866.      line:    64
  1867.               0x80000BEC: func2
  1868.               0x80000C3E: func1
  1869.               0x80000C48: main
  1870.               0x800009E8: _start
  1871.      freed:   0
  1872.      nothing known about address 0x8000A068
  1873.  
  1874.    As you can see, anything that the mpatrol library knows about any
  1875. memory allocation can be obtained for use in your own code, which can
  1876. be very useful if you need to write handlers to keep track of memory
  1877. allocations, etc. for debugging purposes.  It can also be useful to
  1878. have this information when running your program within a debugger, so
  1879. you can use the `__mp_printinfo()' function to display information
  1880. about a heap address if your debugger supports calling functions from
  1881. the command prompt.
  1882.  
  1883.    It is also possible for you to be able to intercept calls to
  1884. allocate, reallocate and deallocate memory for your own purposes.  You
  1885. can install prologue and epilogue functions that the mpatrol library
  1886. will call before and after every time one of its functions is called.
  1887. These can be used for additional tracing or simply to add extra checks
  1888. to your code.  The following code is an example of this and can be
  1889. found in `tests/pass/test2.c'.
  1890.  
  1891.      23  /*
  1892.      24   * Demonstrates and tests the facility for specifying user-defined
  1893.      25   * prologue and epilogue functions.
  1894.      26   */
  1895.      
  1896.      
  1897.      29  #include "mpatrol.h"
  1898.      30  #include <stdio.h>
  1899.      
  1900.      
  1901.      33  void prologue(const void *p, size_t l)
  1902.      34  {
  1903.      35      if (p == (void *) -1)
  1904.      36          fprintf(stderr, "allocating %lu bytes\n", l);
  1905.      37      else if (l == (size_t) -1)
  1906.      38          fprintf(stderr, "freeing allocation 0x%08lX\n", p);
  1907.      39      else if (l == (size_t) -2)
  1908.      40          fprintf(stderr, "duplicating string `%s'\n", p);
  1909.      41      else
  1910.      42          fprintf(stderr, "reallocating allocation 0x%08lX to %lu bytes\n", p, l);
  1911.      43  }
  1912.      
  1913.      
  1914.      46  void epilogue(const void *p)
  1915.      47  {
  1916.      48      if (p != (void *) -1)
  1917.      49          fprintf(stderr, "allocation returns 0x%08lX\n", p);
  1918.      50  }
  1919.      
  1920.      
  1921.      53  int main(void)
  1922.      54  {
  1923.      55      void *p, *q;
  1924.      
  1925.      57      __mp_prologue(prologue);
  1926.      58      __mp_epilogue(epilogue);
  1927.      59      if (p = malloc(16))
  1928.      60          if (q = realloc(p, 32))
  1929.      61              free(q);
  1930.      62          else
  1931.      63              free(p);
  1932.      64      if (p = (char *) strdup("test"))
  1933.      65          free(p);
  1934.      66      return EXIT_SUCCESS;
  1935.      67  }
  1936.  
  1937.    Once again, if you compile and run the above code, you should see
  1938. the following output.
  1939.  
  1940.      allocating 16 bytes
  1941.      allocation returns 0x8000A068
  1942.      reallocating allocation 0x8000A068 to 32 bytes
  1943.      allocation returns 0x8000A068
  1944.      freeing allocation 0x8000A068
  1945.      duplicating string `test'
  1946.      allocation returns 0x8000A068
  1947.      freeing allocation 0x8000A068
  1948.  
  1949.    Along with being able to install prologue and epilogue functions,
  1950. you can also install a low-memory handler with the `__mp_nomemory()'
  1951. function, which will be called by the mpatrol library if it ever runs
  1952. out of memory during the call to a memory allocation function.  This
  1953. gives you the opportunity to use that handler to either free up any
  1954. unneeded memory or simply to abort, thus removing the need to check for
  1955. failed allocations.
  1956.  
  1957.    Finally, there are three functions which affect the mpatrol library
  1958. globally.  The first, `__mp_check()', allows you to force an internal
  1959. check of the mpatrol library's data structures at any point during
  1960. program execution.  The other two functions, `__mp_memorymap()' and
  1961. `__mp_summary()' allow you to force the generation of a memory map or
  1962. library statistics at any point in your program, in much the same way
  1963. as they would normally be displayed at the end of program execution.
  1964.  
  1965. 
  1966. File: mpatrol.info,  Node: Utilities,  Prev: Library functions,  Up: Using mpatrol
  1967.  
  1968. Utilities
  1969. =========
  1970.  
  1971.    A command is provided with the mpatrol distribution which can run
  1972. programs that have been linked with the mpatrol library, using a
  1973. combination of mpatrol options that can be set via the command line.
  1974. All of these options but one map directly onto their equivalent
  1975. environment variable settings and exist mainly so that the user does
  1976. not have to manually change the `MPATROL_OPTIONS' environment variable.
  1977.  
  1978.    The one option that is the exception to this is the `-d' option,
  1979. which can be used to run a program under the control of the mpatrol
  1980. library, even if it wasn't originally linked with the mpatrol library.
  1981. This can only be done on systems that support dynamic linking and where
  1982. the dynamic linker recognises the `LD_PRELOAD' or `_RLD_LIST'
  1983. environment variables.  Even then, it can only be used when the program
  1984. that is being run has been dynamically linked with the system C
  1985. library, rather than statically linked.
  1986.  
  1987.    The reason for all of these limitations is that some SVR4 UNIX
  1988. platforms have a special feature in the dynamic linker which can be
  1989. told to override the symbols from one shared library using the symbols
  1990. from another shared library at run-time.  In this case, it involves
  1991. replacing the symbols for `malloc()', etc., in the system C library
  1992. with the mpatrol versions, but only if they were marked as undefined in
  1993. the original executable file and would therefore have to have been
  1994. loaded from `libc.so'.
  1995.  
  1996.    However, if a program qualifies for use with the `-d' option, it
  1997. means that you can trace all of its dynamic memory allocations as well
  1998. as running it with any of the mpatrol library's debugging options.
  1999. This is mainly a _toy_ feature which allows you to view and manipulate
  2000. the dynamic memory allocations of programs that you don't have the
  2001. source for, but in theory it could be quite useful if you need to debug
  2002. a previously released executable and are unable to recompile or relink
  2003. it.
  2004.  
  2005.    Note that the `mpatrol' command must be set up to use the correct
  2006. object file format access libraries that are required for your system
  2007. if you wish to use the `-d' option.  If the mpatrol library was built
  2008. with `FORMAT=FORMAT_ELF32' support then it must be told to preload the
  2009. ELF access library (normally `libelf.so').  If it was built with
  2010. `FORMAT=FORMAT_BFD' support then it must be told to preload the GNU BFD
  2011. access libraries (normally `libbfd.so' and `libiberty.so').  However,
  2012. if these libraries only exist on your system in archive form then you
  2013. must build `libmpatrol.so' with these extra libraries incorporated into
  2014. it so that there are no dependencies on them at run-time.  However,
  2015. there may well be problems if the resulting shared library contains
  2016. position-dependent code from the archive libraries you incorporated.
  2017. The only way to find out is for you to try it and see.
  2018.  
  2019.    In order to build a shared version of the mpatrol library with
  2020. embedded object file format access libraries, you must first modify the
  2021. `Makefile' you would normally use to build the mpatrol library.  At the
  2022. lines where the linker is invoked to build the shared library, you must
  2023. explicitly add any object file format access libraries that you want to
  2024. use at the end of the linker command line.  This ensures that all
  2025. references to such libraries will be resolved at link time rather than
  2026. run time.  You must then edit the file `src/config.h' and remove all of
  2027. the libraries that you embedded from the definition of the
  2028. `MP_PRELOAD_LIBS' preprocessor macro.  Finally, rebuild the shared
  2029. version of the mpatrol library and the `mpatrol' command and see if
  2030. your efforts were worth it.
  2031.  
  2032.    Another utility program that is provided is called `mleak' and is
  2033. useful for detecting memory leaks in log files produced by the mpatrol
  2034. library.  This program should be used if the mpatrol library could not
  2035. finish writing the log file due to abnormal program termination (which
  2036. would prevent the `SHOWUNFREED' option from working), but note that
  2037. some of the unfreed allocations might have been freed if the program
  2038. had terminated successfully.
  2039.  
  2040.    The `mleak' command scans through an mpatrol log file looking for
  2041. lines beginning with `ALLOC:' and `FREE:' but ignores lines beginning
  2042. with `REALLOC:', so only the `LOGALLOCS' and `LOGFREES' options are
  2043. necessary when running a program linked with the mpatrol library.  Note
  2044. that as a result of this, no attempt is made to account for resizing of
  2045. memory allocations and so the total amount of memory used by the
  2046. resulting unfreed allocations may not be entirely accurate.
  2047.  
  2048.    The `mleak' command takes one optional argument which must be a valid
  2049. mpatrol log filename but if it is omitted then it will use
  2050. `mpatrol.log' as the name of the log file to use.  The `mleak' command
  2051. makes two passes over the log file so the file must be
  2052. randomly-accessible.  If the filename argument is given as `-' then the
  2053. standard input file stream will be used as the log file.
  2054.  
  2055. 
  2056. File: mpatrol.info,  Node: Profiling,  Next: Improving performance,  Prev: Using mpatrol,  Up: Top
  2057.  
  2058. Profiling
  2059. *********
  2060.  
  2061.    The mpatrol library has the capability to summarise the information
  2062. it accumulated about the behaviour of dynamic memory allocations and
  2063. deallocations over the lifetime of any program that it was linked and
  2064. run with.  This summary shows a rough profile of all memory allocations
  2065. that were made, and is hence called _profiling_.  There are several
  2066. other different kinds of profiling provided with most compilation
  2067. tools, but they generally profile function calls or line numbers in
  2068. combination with the time it takes to execute them.
  2069.  
  2070.    Memory allocation profiling is useful since it allows a programmer
  2071. to see which functions directly allocate memory from the heap, with a
  2072. view to optimising the memory usage or performance of a program.  It
  2073. also summarises any unfreed memory allocations that were present at the
  2074. end of program execution, some of which could be as a result of memory
  2075. leaks.  In addition, a summary of the sizes and distribution of all
  2076. memory allocations and deallocations is available.
  2077.  
  2078.    Only allocations and deallocations are recorded, with each
  2079. reallocation being treated as a deallocation immediately followed by an
  2080. allocation.  For full memory allocation profiling support, call stack
  2081. traversal must be supported in the mpatrol library and all of the
  2082. program's symbols must have been successfully read by the mpatrol
  2083. library before the program was run.  The library will attempt to
  2084. compensate if either of these requirements are not met, but the
  2085. displayed tables may contain less meaningful information.
  2086.  
  2087.    Memory allocation profiling is disabled by default, but can be
  2088. enabled using the `PROF' option.  This writes all of the profiling data
  2089. to a file called `mpatrol.out' in the current directory at the end of
  2090. program execution, but the name of this file can be changed using the
  2091. `PROFFILE' option.  Sometimes it can also be desirable for the mpatrol
  2092. library to write out the accumulated profiling information in the
  2093. middle of program execution rather than just at the end, even if it is
  2094. only partially complete, and this behaviour can be controlled with the
  2095. `AUTOSAVE' option.  This can be particularly useful when running the
  2096. program from within a debugger, when it is necessary to analyse the
  2097. profiling information at a certain point during program execution.
  2098.  
  2099.    When profiling memory allocations, it is necessary to distinguish
  2100. between small, medium, large and extra large memory allocations that
  2101. were made by a function.  The boundaries which distinguish between
  2102. these allocation sizes can be controlled via the `SMALLBOUND',
  2103. `MEDIUMBOUND' and `LARGEBOUND' options, but they default to 32, 256 and
  2104. 2048 bytes respectively, which should suffice for most circumstances.
  2105.  
  2106.    The `mprof' command is a tool designed to read a profiling output
  2107. file produced by the mpatrol library and display the profiling
  2108. information that was obtained.  The profiling information includes
  2109. summaries of all of the memory allocations listed by size and the
  2110. function that allocated them and a list of memory leaks with the call
  2111. stack of the allocating function.
  2112.  
  2113.    Along with the options listed below, the `mprof' command takes one
  2114. optional argument which must be a valid mpatrol profiling output
  2115. filename but if it is omitted then it will use `mpatrol.out' as the
  2116. name of the file to use.  If the filename argument is given as `-' then
  2117. the standard input file stream will be used as the profiling output
  2118. file.
  2119.  
  2120. `-a'
  2121.      Specifies that different call sites from within the same function
  2122.      are to be differentiated and that the names of all functions
  2123.      should be displayed with their call site offset in bytes.  This
  2124.      affects the direct allocation and memory leak tables.
  2125.  
  2126. `-c'
  2127.      Specifies that certain tables should be sorted by the number of
  2128.      allocations or deallocations rather than the total number of bytes
  2129.      allocated or deallocated.  This affects the direct allocation and
  2130.      memory leak tables.
  2131.  
  2132. `-n' <DEPTH>
  2133.      Specifies the maximum stack depth to use when calculating if one
  2134.      call site has the same call stack as another call site.  This also
  2135.      specifies the maximum number of functions to display in a call
  2136.      stack.  If DEPTH is `0' then the call stack depth will be
  2137.      unlimited in size.  The default call stack depth is `1'.  This
  2138.      affects the memory leak table.
  2139.  
  2140. `-V'
  2141.      Displays the version number of the `mprof' command.
  2142.  
  2143.    We'll now look at an example of using the mpatrol library to profile
  2144. the dynamic memory allocations in a program.  However, remember that
  2145. this example will only fully work on your machine if the mpatrol
  2146. library supports call stack traversal and reading symbols from
  2147. executable files on that platform.  If that is not the case then only
  2148. some of the features will be available.
  2149.  
  2150.    The following example program performs some simple calculations and
  2151. displays a list of numbers on its standard output file stream, but it
  2152. serves to illustrate all of the different features of memory allocation
  2153. profiling that mpatrol is capable of.  The source for the program can
  2154. be found in `tests/profile/test1.c'.
  2155.  
  2156.      23  /*
  2157.      24   * Associates an integer value with its negative string equivalent in a
  2158.      25   * structure, and then allocates 256 such pairs randomly, displays them
  2159.      26   * then frees them.
  2160.      27   */
  2161.      
  2162.      
  2163.      30  #include <stdio.h>
  2164.      31  #include <stdlib.h>
  2165.      32  #include <string.h>
  2166.      
  2167.      
  2168.      35  typedef struct pair
  2169.      36  {
  2170.      37      int value;
  2171.      38      char *string;
  2172.      39  }
  2173.      40  pair;
  2174.      
  2175.      
  2176.      43  pair *new_pair(int n)
  2177.      44  {
  2178.      45      static char s[16];
  2179.      46      pair *p;
  2180.      
  2181.      48      if ((p = (pair *) malloc(sizeof(pair))) == NULL)
  2182.      49      {
  2183.      50          fputs("Out of memory\n", stderr);
  2184.      51          exit(EXIT_FAILURE);
  2185.      52      }
  2186.      53      p->value = n;
  2187.      54      sprintf(s, "%d", -n);
  2188.      55      if ((p->string = strdup(s)) == NULL)
  2189.      56      {
  2190.      57          fputs("Out of memory\n", stderr);
  2191.      58          exit(EXIT_FAILURE);
  2192.      59      }
  2193.      60      return p;
  2194.      61  }
  2195.      
  2196.      
  2197.      64  int main(void)
  2198.      65  {
  2199.      66      pair *a[256];
  2200.      67      int i, n;
  2201.      
  2202.      69      for (i = 0; i < 256; i++)
  2203.      70      {
  2204.      71          n = (int) ((rand() * 256.0) / (RAND_MAX + 1.0)) - 128;
  2205.      72          a[i] = new_pair(n);
  2206.      73      }
  2207.      74      for (i = 0; i < 256; i++)
  2208.      75          printf("%3d: %4d -> \"%s\"\n", i, a[i]->value, a[i]->string);
  2209.      76      for (i = 0; i < 256; i++)
  2210.      77          free(a[i]);
  2211.      78      return EXIT_SUCCESS;
  2212.      79  }
  2213.  
  2214.    After the above program has been compiled and linked with the
  2215. mpatrol library, it should be run with the `PROF' option set in the
  2216. `MPATROL_OPTIONS' environment variable.  Note that `mpatrol.h' was not
  2217. included as it is not necessary for profiling purposes.
  2218.  
  2219.    If all went well, a list of numbers should be displayed on the
  2220. screen and a file called `mpatrol.out' should have been produced in the
  2221. current directory.  This is a binary file containing the total amount
  2222. of profiling information that the mpatrol library gathered while the
  2223. program was running, but it contains concise numerical data rather than
  2224. human-readable data.  To make use of this file, the `mprof' command
  2225. must be run.  An excerpt from the output produced when running `mprof'
  2226. with no options is shown below.
  2227.  
  2228.                                   ALLOCATION BINS
  2229.      
  2230.                                (number of bins: 1024)
  2231.      
  2232.                        allocated                          unfreed
  2233.             --------------------------------  --------------------------------
  2234.       size   count       %     bytes       %   count       %     bytes       %
  2235.      
  2236.          2       8    1.56        16    0.54       8    3.12        16    1.70
  2237.          3      99   19.34       297    9.94      99   38.67       297   31.60
  2238.          4     118   23.05       472   15.80     118   46.09       472   50.21
  2239.          5      31    6.05       155    5.19      31   12.11       155   16.49
  2240.          8     256   50.00      2048   68.54       0    0.00         0    0.00
  2241.      
  2242.      total     512              2988             256               940
  2243.  
  2244.                                  DIRECT ALLOCATIONS
  2245.      
  2246.                       (0 < s <= 32 < m <= 256 < l <= 2048 < x)
  2247.      
  2248.             allocated                       unfreed
  2249.      --------------------------  --------------------------
  2250.      bytes       %   s  m  l  x  bytes       %   s  m  l  x  count  function
  2251.      
  2252.       2988  100.00  %%             940  100.00  %%             512  new_pair
  2253.      
  2254.       2988          %%             940          %%             512  total
  2255.  
  2256.                                     MEMORY LEAKS
  2257.      
  2258.                               (maximum stack depth: 1)
  2259.      
  2260.                      unfreed                      allocated
  2261.      ----------------------------------------  ----------------
  2262.           %     bytes       %   count       %     bytes   count  function
  2263.      
  2264.      100.00       940   31.46     256   50.00      2988     512  new_pair
  2265.      
  2266.                   940   31.46     256   50.00      2988     512  total
  2267.  
  2268.    The first table shown is the allocation bin table which summarises
  2269. the sizes of all objects that were dynamically allocated throughout the
  2270. lifetime of the program.  In this particular case, counts of all
  2271. allocations and deallocations of sizes 1 to 1023 bytes were recorded by
  2272. the mpatrol library in their own specific _bin_ and this information
  2273. was written to the profiling output file.  Allocations and
  2274. deallocations of sizes larger than or equal to 1024 bytes are counted
  2275. as well and the total number of bytes that they represent are also
  2276. recorded.  This information can be extremely useful in understanding
  2277. which sizes of data structures are allocated most during program
  2278. execution, and where changes might be made to make more efficient use
  2279. of the dynamically allocated memory.
  2280.  
  2281.    As can be seen from the allocation bin table, 8 allocations of 2
  2282. bytes, 99 allocations of 3 bytes, 118 allocations of 4 bytes, 31
  2283. allocations of 5 bytes and 256 allocations of 8 bytes were made during
  2284. the execution of the program.  However, all of these memory allocations
  2285. except the 8 byte allocations were still not freed by the time the
  2286. program terminated, resulting in a total memory leak of 940 bytes.
  2287.  
  2288.    The next table shown is the direct allocation table which lists all
  2289. of the functions that allocated memory and how much memory they
  2290. allocated.  The `s m l x' columns represent _small_, _medium_, _large_
  2291. and _extra large_ memory allocations, which in this case are 0 bytes is
  2292. less than a small allocation, which is less than or equal to 32 bytes,
  2293. which is less than a medium allocation, which is less than or equal to
  2294. 256 bytes, which is less than a large allocation, which is less than or
  2295. equal to 2048 bytes, which is less than an extra large allocation.  The
  2296. numbers listed under these columns represent a percentage of the
  2297. overall total and are listed as `%%' if the percentage is 100% or as
  2298. `.' if the percentage is less than 1%.  Percentages of 0% are not
  2299. displayed.
  2300.  
  2301.    The information displayed in the direct allocation table is useful
  2302. for seeing exactly which functions in a program directly perform memory
  2303. allocation, and can quickly highlight where optimisations can be made
  2304. or where functions might be making unnecessary allocations.  In the
  2305. example, this table shows us that 2988 bytes were allocated over 512
  2306. calls by `new_pair()' and that 940 bytes were left unfreed at program
  2307. termination.  All of the allocations that were made by `new_pair()'
  2308. were between 1 and 32 bytes in size.
  2309.  
  2310.    We could now choose to sort the direct allocation table by the
  2311. number of calls to allocate memory, rather than the number of bytes
  2312. allocated, with the `-c' option to `mprof', but that is not relevant in
  2313. this example.  However, we know that there are two calls to allocate
  2314. memory from `new_pair()', so we can use the `-a' option to `mprof' to
  2315. show all call sites within functions rather than just the total for each
  2316. function.  This option does not affect the allocation bin table so the
  2317. new output from `mprof' with the `-a' option looks like:
  2318.  
  2319.                                  DIRECT ALLOCATIONS
  2320.      
  2321.                       (0 < s <= 32 < m <= 256 < l <= 2048 < x)
  2322.      
  2323.             allocated                       unfreed
  2324.      --------------------------  --------------------------
  2325.      bytes       %   s  m  l  x  bytes       %   s  m  l  x  count  function
  2326.      
  2327.       2048   68.54  69               0    0.00                 256  new_pair+14
  2328.        940   31.46  31             940  100.00  %%             256  new_pair+110
  2329.      
  2330.       2988          %%             940          %%             512  total
  2331.  
  2332.                                     MEMORY LEAKS
  2333.      
  2334.                               (maximum stack depth: 1)
  2335.      
  2336.                      unfreed                      allocated
  2337.      ----------------------------------------  ----------------
  2338.           %     bytes       %   count       %     bytes   count  function
  2339.      
  2340.      100.00       940  100.00     256  100.00       940     256  new_pair+110
  2341.      
  2342.                   940   31.46     256   50.00      2988     512  total
  2343.  
  2344.    The names of the functions displayed in the above tables now have a
  2345. byte offset appended to them to indicate at what position in the
  2346. function a call to allocate memory occurred(1).  Now it is possible to
  2347. see that the first call to allocate memory from within `new_pair()' has
  2348. had all of its memory freed, but the second call (from `strdup()') has
  2349. had none of its memory freed.
  2350.  
  2351.    This is also visible in the next table, which is the memory leak
  2352. table and lists all of the functions that allocated memory but did not
  2353. free all of their memory during the lifetime of the program.  The
  2354. default behaviour of `mprof' is to show only the function that directly
  2355. allocated the memory in the memory leak table, but this can be changed
  2356. with the `-n' option.  This accepts an argument specifying the maximum
  2357. number of functions to display in one call stack, with zero indicating
  2358. that all functions in a call stack should be displayed.  This can be
  2359. useful for tracing down the functions that were indirectly responsible
  2360. for the memory leak.  The new memory leak table displayed by `mprof'
  2361. with the `-a' and `-n0' options looks like:
  2362.  
  2363.                                     MEMORY LEAKS
  2364.      
  2365.                               (maximum stack depth: 0)
  2366.      
  2367.                      unfreed                      allocated
  2368.      ----------------------------------------  ----------------
  2369.           %     bytes       %   count       %     bytes   count  function
  2370.      
  2371.      100.00       940  100.00     256  100.00       940     256  new_pair+110
  2372.                                                                  main+88
  2373.                                                                  _start+68
  2374.      
  2375.                   940   31.46     256   50.00      2988     512  total
  2376.  
  2377.    Now that we know where the memory leak is coming from, we can fix it
  2378. by freeing the string as well as the structure at line 77.  A version
  2379. of the above program that does not contain the memory leak can be found
  2380. in `tests/profile/test2.c'.
  2381.  
  2382.    Much of the functionality of this implementation of memory
  2383. allocation profiling is based upon `mprof' by Benjamin Zorn and Paul
  2384. Hilfinger, which was written as a research project and ran on MIPS,
  2385. SPARC and VAX machines.  However, the profiling output files are
  2386. incompatible, the tables displayed have a different format, and the way
  2387. they are implemented is entirely different.
  2388.  
  2389.    ---------- Footnotes ----------
  2390.  
  2391.    (1) If no symbols could be read from the program's executable file,
  2392. or if the corresponding symbol could not be determined, then the
  2393. function names will be replaced with the code addresses at which the
  2394. calls took place.
  2395.  
  2396. 
  2397. File: mpatrol.info,  Node: Improving performance,  Next: How it works,  Prev: Profiling,  Up: Top
  2398.  
  2399. Improving performance
  2400. *********************
  2401.  
  2402.    Because of their need to cover every eventuality, malloc library
  2403. implementations are very general and most do their job well when you
  2404. consider what is thrown at them.  However, your program may not be
  2405. performing as well as it should simply because there may be a more
  2406. efficient way of dealing with dynamic memory allocations.  Indeed,
  2407. there may even be a more efficient malloc library available for you to
  2408. use.
  2409.  
  2410.    If you need to allocate lots of blocks of the same size(1), but you
  2411. won't know the number of blocks you'll require until run-time then you
  2412. could take the easy approach by simply allocating a new block of memory
  2413. for each occurrence.  However, this is going to create a lot of
  2414. (typically small) memory blocks that the underlying malloc library will
  2415. have to keep track of, and even in many good malloc libraries this is
  2416. likely to cause memory fragmentation and possibly even result in the
  2417. blocks scattered throughout the address space rather than all in the
  2418. one place, which is not necessarily a good thing on systems with
  2419. virtual memory.
  2420.  
  2421.    An alternative approach would be to allocate memory in multiples of
  2422. the block size, so that several blocks would be allocated at once.
  2423. This would require slightly more work on your part since you would need
  2424. to write interface code to return a single block, while possible
  2425. allocating space for more blocks if no free blocks were available.
  2426. However, this approach has several advantages.  The first is that the
  2427. malloc library only needs to keep track of a few large allocations
  2428. rather than lots of small allocations, so splitting and merging free
  2429. blocks is less likely to occur.  Secondly, your blocks will be
  2430. scattered about less in the address space of the process, which means
  2431. that on systems with virtual memory there are less likely to be page
  2432. faults if you need to access or traverse all of the blocks you have
  2433. created.
  2434.  
  2435.    A memory allocation concept that is similar to this is called an
  2436. _arena_.  This datatype requires functions which are built on top of
  2437. the existing malloc library functions and which associate each memory
  2438. allocation with a particular arena.  An arena can have as many
  2439. allocations added to it as required, but allocations cannot usually be
  2440. freed until the whole arena is freed.  Note that there are not really
  2441. any generic implementations of arenas that are available as everyone
  2442. tends to write their own version when they require it, although Digital
  2443. UNIX and SGI IRIX systems do come with an arena library called
  2444. _amalloc_.
  2445.  
  2446.    However, what if you don't plan to free all of the blocks at the
  2447. same time?  A slight modification to the above design could be to have
  2448. a _slot table_.  This would involve allocating chunks of blocks as they
  2449. are required, adding each individual block within a chunk to a
  2450. singly-linked list of free blocks.  Then, as new blocks are required,
  2451. the allocator would simply choose the first block on the free list,
  2452. otherwise it would allocate memory for a new chunk of blocks and add
  2453. them to the free list.  Freeing individual blocks would simply involve
  2454. returning the block to the free list.  If this description isn't clear
  2455. enough, have a look in `src/slots.h' and `src/slots.c'.  This is how the
  2456. mpatrol library allocates memory from the system for all of its internal
  2457. structures.  For variable-sized structures, a slightly different
  2458. approach needs to be taken, but for an example of this using strings
  2459. see `src/strtab.h' and `src/strtab.c'.
  2460.  
  2461.    Another optimisation that is possible on UNIX and Windows platforms
  2462. is making use of memory-mapped files.  This allows you to map a
  2463. filesystem object into the address space of your process, thus allowing
  2464. you to treat a file as an array of bytes.  Because it uses the virtual
  2465. memory system to map the file, any changes you make to the mapped
  2466. memory will be applied to the file.  This is implemented through the
  2467. virtual memory system treating the file as a pseudo swap file and will
  2468. therefore only use up physical memory when pages are accessed.  It also
  2469. means that file operations can be replaced by memory read and write
  2470. operations, leading to a very fast and efficient way of performing I/O.
  2471. Another added bonus of this system means that entire blocks of process
  2472. memory can be written to a file for later re-use, just as long as the
  2473. file can later be mapped to the same address.  This can be a lot faster
  2474. than writing to and reading from a specific format of file.
  2475.  
  2476.    If you really don't want to keep track of dynamic memory allocations
  2477. at all then perhaps you should consider _garbage collection_.  This
  2478. allows you to make dynamic memory allocations that need not necessarily
  2479. be matched by corresponding calls to free these allocations.  A garbage
  2480. collector will (at certain points during program execution) attempt to
  2481. look for memory allocations that are no longer referenced by the
  2482. program and free them for later re-use, hence removing all possibility
  2483. of memory leaks.  However, the garbage collection process can take a
  2484. sizable chunk of processor time depending on how large the program is,
  2485. so it is not really an option for real-time programming.  It is also
  2486. very platform-dependent as it examines very low-level structures within
  2487. a process in order to determine which pointers point to which memory
  2488. allocations.  But there is at least one garbage collector(2) that works
  2489. well with C and C++ and acts as a replacement for `malloc()' and
  2490. `free()', so it may be the ideal solution for you.
  2491.  
  2492.    If you do choose to use an alternative malloc library make sure that
  2493. you have a license to do so and that you follow any distribution
  2494. requirements.  On systems that support dynamic linking you may want to
  2495. link the library statically rather than dynamically so that you don't
  2496. have to worry about an additional file that would need to be installed.
  2497. However, whether you have that choice depends on the license for the
  2498. specific library, and some licenses also require that the source code
  2499. for the library be made readily available.  Shared libraries have the
  2500. advantage that they can be updated with bug fixes so that all programs
  2501. that require these libraries will automatically receive these fixes
  2502. without needing to be relinked.
  2503.  
  2504.    If all of the above suggestions do not seem to help and you still
  2505. feel that you have a performance bottleneck in the part of your code
  2506. that deals with dynamically allocated memory then you should try using
  2507. the memory allocation profiling feature of mpatrol.  This can be used
  2508. at run-time to analyse the dynamic memory allocation calls that your
  2509. program makes during its execution, and builds statistics for later
  2510. viewing with the `mprof' command.  It is then possible for you to see
  2511. exactly how many calls were made to each function and where they came
  2512. from.  Such information can then be put to good use in order to
  2513. optimise the relevant parts of your code.
  2514.  
  2515.    And finally, some tips on how to correctly use dynamic memory
  2516. allocations.  The first, most basic rule is to _always_ check the
  2517. return values from `malloc()' and related functions.  _Never_ assume
  2518. that a call to `malloc()' will succeed, because you're unlikely to be
  2519. able to read the future(3).  Alternatively, use (or write) an
  2520. `xmalloc()' or similar function, which calls `malloc()' but never
  2521. returns `NULL' since it will abort instead.  With the C++ operators it
  2522. is slightly different because some versions use exceptions to indicate
  2523. failure, so you should always provide a handler to deal with this
  2524. eventuality.
  2525.  
  2526.    Never use _features_(4) of specific malloc libraries if you want
  2527. your code to be portable.  Always follow the ANSI C or C++ calling
  2528. conventions and never make assumptions about the function or operator
  2529. you are about to call -- the standards committees went to great lengths
  2530. to explicitly specify its behaviour.  For example, don't assume that
  2531. the contents of a freed memory allocation will remain valid until the
  2532. next call to `malloc()', and don't assume that the contents of a newly
  2533. allocated memory block will be zeroed unless you created it with
  2534. `calloc()'.
  2535.  
  2536.    Finally, try stress-testing your program in low memory conditions.
  2537. The mpatrol library contains the `LIMIT' option which can place an
  2538. upper bound on the size of the heap, and also contains the `FAILFREQ'
  2539. and `FAILSEED' options which can cause random memory allocation
  2540. failures.  Doing this will test parts of your code that you would
  2541. probably never expect to be called, but perhaps they will one day!  Who
  2542. would you rather have debugging your program -- yourself or the user?
  2543.  
  2544.    ---------- Footnotes ----------
  2545.  
  2546.    (1) Such as for use in a linked list.
  2547.  
  2548.    (2) A freely distributably library called GC (*note Related
  2549. software::).
  2550.  
  2551.    (3) If you can, why are you reading this -- you've already read it!
  2552.  
  2553.    (4) Whether they are documented or not.
  2554.  
  2555. 
  2556. File: mpatrol.info,  Node: How it works,  Next: Examples,  Prev: Improving performance,  Up: Top
  2557.  
  2558. How it works
  2559. ************
  2560.  
  2561.    The mpatrol library was originally written with the intention of
  2562. plugging it into an existing compiler so that the compiler could plant
  2563. calls to it in the code it generated when a specific debugging option
  2564. was used.  These extra calls would obviously slow the code down, but
  2565. along with the stack checking options that would be provided, this
  2566. would give the user an enhanced run-time debugging environment.
  2567. Unfortunately, this integration never happened, but the way that
  2568. mpatrol works is still significantly different from other malloc tracing
  2569. libraries.
  2570.  
  2571.    In order to quickly determine exactly which memory allocation a heap
  2572. address belonged to it was necessary to be able to search the heap in
  2573. an efficient manner.  The traditional way of searching along a linked
  2574. list was unfeasible, so an implementation based on _red-black trees_
  2575. was used, where every known memory allocation in the heap was given an
  2576. entry in the tree, with their start addresses as the key.  Another
  2577. major design decision was to also choose red-black trees to implement
  2578. the _best fit_ allocation algorithm.  Although _first fit_ was
  2579. considered, I decided that best fit would allow the library to have
  2580. more control over the heap, with every free memory block in the heap
  2581. given an entry in the free tree, with their sizes as the key.  There
  2582. was a bit of work involved in getting the splitting and merging of free
  2583. blocks to work efficiently, but it seems to work well now.
  2584.  
  2585.    My original implementation had all of the information about each
  2586. memory block stored just before the block itself.  I eventually dropped
  2587. that behaviour in favour of storing all of the library's internal
  2588. information in a separate part of the heap.  I did that for two
  2589. reasons.  The first was because of the problems that would occur due to
  2590. memory allocations with different alignment requirements.  The second
  2591. reason was that the library's internal structures could be
  2592. write-protected on systems with virtual memory, to prevent user code
  2593. interfering with the operation of the library.
  2594.  
  2595.    Because the library attempts to record as much information as
  2596. possible about every memory allocation there will inevitably be a much
  2597. larger memory requirement when running a program linked with the
  2598. library.  This will typically be two or three times larger in
  2599. magnitude, but will be affected by the number of memory allocations
  2600. made and also the number of symbols read.  The latter will also affect
  2601. how quickly the program starts since the first call to allocate memory
  2602. will result in the initialisation of the library and the loading of
  2603. symbols from the executable file and any shared libraries.
  2604.  
  2605.    Due to its design, it is also possible to allocate memory from the
  2606. heap using the mpatrol library functions whilst already within an
  2607. mpatrol library function.  This does not normally occur, but on some
  2608. platforms calling `printf()' from within the library may result in
  2609. `printf()' calling `malloc()' to allocate itself a buffer, which ends
  2610. up as a recursive call.  Luckily, this is dealt with by simply not
  2611. displaying the allocation in the log file, but all other details of the
  2612. allocation are still recorded.  This can sometimes result in _hidden_
  2613. memory usage which occurs behind the scenes and alters the peak memory
  2614. usage in the summary.  This is particularly evident when the library
  2615. uses an object file access library to read program symbols at the time
  2616. of library initialisation.
  2617.  
  2618.    Memory allocation profiling support was added for mpatrol release
  2619. 1.2.0.  Every allocation and deallocation is recorded, with the call
  2620. stack information being used to differentiate all of the call sites
  2621. within the program.  Unlike other profilers that come with UNIX
  2622. systems, even the symbolic information about the program being run is
  2623. written to the profiling output file, since it makes no sense for
  2624. `mprof' to re-read the symbol table from the executable file when it
  2625. has already been read and processed by the mpatrol library.  It also has
  2626. the added bonus of allowing the user to save profiling output files for
  2627. later use even when the executable files which produced them have
  2628. changed or no longer exist.
  2629.  
  2630.    The library is written in a modular fashion so as to make it easy to
  2631. add new functionality.  New modules have already been added, such as
  2632. the _stack_, _symbol_ and _profile_ modules.  Extra information about
  2633. each memory allocation can be added to the _allocation information_
  2634. module in `src/info.h' and `src/info.c' without having to change much
  2635. code in any other files.
  2636.  
  2637. 
  2638. File: mpatrol.info,  Node: Examples,  Next: Tutorial,  Prev: How it works,  Up: Top
  2639.  
  2640. Examples
  2641. ********
  2642.  
  2643.    Following are a set of examples that are intended to illustrate what
  2644. exactly is possible with the mpatrol library and how to go about using
  2645. it effectively.
  2646.  
  2647.    You should already have built and installed the library and should
  2648. know how to link programs with the library.  Unfortunately, it isn't
  2649. possible to give specific instructions on how to do this as it varies
  2650. from system to system and also depends on your preferred compiler and
  2651. development tools.
  2652.  
  2653.    However, on a typical SVR4 UNIX system, with mpatrol installed in
  2654. `/usr/local', the mpatrol library can usually be incorporated into a
  2655. program using the following commands:
  2656.  
  2657.    * If the mpatrol library was built with no support for any object
  2658.      file format or was built with support for the COFF object file
  2659.      format:
  2660.  
  2661.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol
  2662.  
  2663.    * If the mpatrol library was built with support for the ELF32 object
  2664.      file format access library:
  2665.  
  2666.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol -lelf
  2667.  
  2668.    * If the mpatrol library was built with support for the GNU BFD
  2669.      object file format access library:
  2670.  
  2671.           cc -I/usr/local/include <file> -L/usr/local/lib -lmpatrol -lbfd
  2672.              -liberty
  2673.  
  2674.    If you need to link with other libraries, make sure that they don't
  2675. contain definitions of `malloc()', etc., or if they do then you must
  2676. ensure that the mpatrol library appears before them on the link line.
  2677.  
  2678.    You should also know how to set an environment variable on your
  2679. specific system.  Again, this varies from system to system and also
  2680. depends on the command line interpreter or shell that you use.  The
  2681. environment variable that the mpatrol library uses is called
  2682. `MPATROL_OPTIONS'.  You can see exactly what options are available for
  2683. this environment variable by setting it to `HELP' and then running a
  2684. program that has been linked with the library.
  2685.  
  2686. * Menu:
  2687.  
  2688. * Example 1::                   Getting started.
  2689. * Example 2::                   Detecting incorrect reuse of freed memory.
  2690. * Example 3::                   Detecting use of free memory.
  2691. * Example 4::                   Using overflow buffers.
  2692. * Example 5::                   Bad memory operations.
  2693. * Example 6::                   Incompatible function calls.
  2694. * Example 7::                   Additional useful information.
  2695.  
  2696. 
  2697. File: mpatrol.info,  Node: Example 1,  Next: Example 2,  Up: Examples
  2698.  
  2699. Getting started
  2700. ===============
  2701.  
  2702.    The first example we'll look at is when the argument in a call to
  2703. `free()' doesn't match the return value from `malloc()', even though
  2704. the intention is to free the memory that was allocated by `malloc()'.
  2705. This example is in `tests/fail/test1.c' and causes many existing
  2706. `malloc()' implementations to crash.
  2707.  
  2708.    Along the way, I'll try to describe as many features of the mpatrol
  2709. library as possible, and illustrate them with examples.  Note that the
  2710. output from your version of the library is likely to vary slightly from
  2711. that shown in the examples, especially on non-UNIX systems.
  2712.  
  2713.      23  /*
  2714.      24   * Allocates a block of 16 bytes and then attempts to free the
  2715.      25   * memory returned at an offset of 1 byte into the block.
  2716.      26   */
  2717.      
  2718.      
  2719.      29  #include "mpatrol.h"
  2720.      
  2721.      
  2722.      32  int main(void)
  2723.      33  {
  2724.      34      char *p;
  2725.      
  2726.      36      if (p = (char *) malloc(16))
  2727.      37          free(p + 1);
  2728.      38      return EXIT_SUCCESS;
  2729.      39  }
  2730.  
  2731.    Note that I've removed the copyright message from the start of the
  2732. file and added line numbers so that the tracing below makes more sense.
  2733.  
  2734.    After compiling and linking the above program with the mpatrol
  2735. library, the `MPATROL_OPTIONS' environment variable should be set to be
  2736. `LOGALL' and the program should be executed, generating the following
  2737. output in `mpatrol.log'.
  2738.  
  2739.      @(#) mpatrol 1.2.0 (00/05/16)
  2740.      Copyright (C) 1997-2000 Graeme S. Roy
  2741.      
  2742.      This is free software, and you are welcome to redistribute it under
  2743.      certain conditions; see the GNU Library General Public License for
  2744.      details.
  2745.      
  2746.      For the latest mpatrol release and documentation,
  2747.      visit http://www.cbmamiga.demon.co.uk/mpatrol.
  2748.      
  2749.      Log file generated on Tue May  2 23:41:04 2000
  2750.      
  2751.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test1.c|36]
  2752.              0x00010AE0 main
  2753.              0x000109D4 _start
  2754.      
  2755.      returns 0x00028000
  2756.      
  2757.      FREE: free (0x00028001) [main|test1.c|37]
  2758.              0x00010B24 main
  2759.              0x000109D4 _start
  2760.      
  2761.      ERROR: free: 0x00028001 does not match allocation of 0x00028000
  2762.          0x00028000 (16 bytes) {malloc:13:0} [main|test1.c|36]
  2763.              0x00010AE0 main
  2764.              0x000109D4 _start
  2765.      
  2766.      system page size:  8192 bytes
  2767.      default alignment: 8 bytes
  2768.      overflow size:     0 bytes
  2769.      overflow byte:     0xAA
  2770.      allocation byte:   0xFF
  2771.      free byte:         0x55
  2772.      allocation stop:   0
  2773.      reallocation stop: 0
  2774.      free stop:         0
  2775.      unfreed abort:     0
  2776.      small boundary:    32
  2777.      medium boundary:   256
  2778.      large boundary:    2048
  2779.      lower check range: -
  2780.      upper check range: -
  2781.      failure frequency: 0
  2782.      failure seed:      533453
  2783.      prologue function: <unset>
  2784.      epilogue function: <unset>
  2785.      handler function:  <unset>
  2786.      log file:          mpatrol.log
  2787.      profiling file:    mpatrol.out
  2788.      program filename:  ./test1
  2789.      symbols read:      3240
  2790.      autosave count:    0
  2791.      allocation count:  13
  2792.      allocation peak:   4720 bytes
  2793.      allocation limit:  0 bytes
  2794.      allocated blocks:  1 (16 bytes)
  2795.      freed blocks:      0 (0 bytes)
  2796.      free blocks:       1 (8176 bytes)
  2797.      internal blocks:   25 (204800 bytes)
  2798.      total heap usage:  212992 bytes
  2799.      total compared:    0 bytes
  2800.      total located:     0 bytes
  2801.      total copied:      0 bytes
  2802.      total set:         0 bytes
  2803.      total warnings:    0
  2804.      total errors:      1
  2805.  
  2806.    Ignoring the copyright blurb at the top, let's first take a look at
  2807. the initial log message from the library.  I've annotated each of the
  2808. items with a number that corresponds to the descriptions below.
  2809.  
  2810.       (1)    (2)   (3)    (4)       (5)     (6)    (7)  (8)
  2811.        |      |     |      |         |       |      |    |
  2812.        V      V     V      V         V       V      V    V
  2813.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test1.c|36]
  2814.       (9) -> 0x00010AE0 main
  2815.              0x000109D4 _start <- (10)
  2816.      
  2817.      returns 0x00028000 <- (11)
  2818.  
  2819.   1. Allocation type.  This generalises the type of dynamic memory
  2820.      operation that is being performed, and can be one of `ALLOC',
  2821.      `REALLOC' or `FREE'.  This should make looking for all
  2822.      allocations, reallocations or frees in the log file a lot easier.
  2823.      Alternatively, if a memory operation function was called then this
  2824.      can also be one of `MEMSET', `MEMCOPY', `MEMFIND' or `MEMCMP'.
  2825.  
  2826.   2. Allocation function.  This is the name of the function that has
  2827.      been called to allocate the memory, in this case `malloc'.
  2828.  
  2829.   3. Allocation index.  This is incremented every time a new memory
  2830.      allocation is requested, and persists even if the memory
  2831.      allocation is resized with `realloc()', `recalloc()' or
  2832.      `expand()', so can be useful to keep track of a memory allocation,
  2833.      even if its start address changes.  The mpatrol library may use up
  2834.      the first few allocation indices when it gets initialised.
  2835.  
  2836.   4. Size of requested allocation.
  2837.  
  2838.   5. Alignment for requested allocation.  This is normally the default
  2839.      system alignment for general-purpose memory allocations, but may
  2840.      be different depending on the type of function that is used to
  2841.      allocate the memory.
  2842.  
  2843.    The following information contains source file details of where the
  2844. call to `malloc()' came from, but is only available if the source file
  2845. containing the call to `malloc()' included `mpatrol.h'; otherwise the
  2846. fields will all be `-'(1).  Because of the convoluted way this
  2847. information is obtained for the C++ operators, you may encounter some
  2848. problems in existing C++ programs when making direct calls to `operator
  2849. new' for example.  However, if you want to disable the redefinition of
  2850. the C++ operators in `mpatrol.h' you can define the preprocessor macro
  2851. `MP_NOCPLUSPLUS' before the inclusion of that file.
  2852.  
  2853.   6. Function where call to `malloc()' took place.  This information is
  2854.      only available if the source file containing the call to
  2855.      `malloc()' was compiled with `gcc' or `g++'.
  2856.  
  2857.   7. Filename in which call to `malloc()' took place.
  2858.  
  2859.   8. Line number at which call to `malloc()' took place.
  2860.  
  2861.    The following information contains function call stack details of
  2862. where the call to `malloc()' came from, but is only available if the
  2863. mpatrol library has been built on a platform that supports this.  The
  2864. top-most entry should be the function which called `malloc()' and the
  2865. bottom-most entry should be the entry-point for the process.
  2866.  
  2867.   9. Address of function call.  This is normally the address of the
  2868.      machine instruction immediately after the function call
  2869.      instruction, also known as the return address.
  2870.  
  2871.  10. Function where call took place.  This information is only
  2872.      available if the mpatrol library has been built on a platform that
  2873.      supports reading symbol table information from executable files,
  2874.      and then only if there is an entry in the symbol table
  2875.      corresponding to the return address.  C++ function names may still
  2876.      be in their mangled form, but this can be easily rectified by
  2877.      processing the log file with a C++ name demangler.
  2878.  
  2879.    The following information is only available when the allocation type
  2880. is `ALLOC' or `REALLOC' since it makes no sense when applied to `FREE'.
  2881.  
  2882.  11. The address of the new memory block that has been allocated by
  2883.      `malloc()'.
  2884.  
  2885.    As you can see, there is quite a lot of information that can be
  2886. displayed from a simple call to `malloc()', and hopefully this
  2887. information has been presented in a clear and concise format in the log
  2888. file.
  2889.  
  2890.    The next entries in the log file correspond to the call to `free()',
  2891. which attempts to free the memory allocated by `malloc()', but supplies
  2892. the wrong address.
  2893.  
  2894.    The first three lines should be self-explanatory as they are very
  2895. similar to those described above for `malloc()'.  However, the next
  2896. lines signal that a terminal error has occurred in the program, so I've
  2897. annotated them as before.
  2898.  
  2899.      FREE: free (0x00028001) [main|test1.c|37]
  2900.              0x00010B24 main
  2901.              0x000109D4 _start
  2902.      
  2903.       (1)   (2)
  2904.        |     |
  2905.        V     V
  2906.      ERROR: free: 0x00028001 does not match allocation of 0x00028000
  2907.             (3)        (4)       (5)  (6)(7)  (8)    (9)  (10)
  2908.              |          |         |    |  |    |      |    |
  2909.              V          V         V    V  V    V      V    V
  2910.          0x00028000 (16 bytes) {malloc:13:0} [main|test1.c|36]
  2911.      (11) -> 0x00010AE0 main
  2912.              0x000109D4 _start
  2913.  
  2914.   1. Error severity.  The mpatrol library has two different severities
  2915.      of error: `WARNING' and `ERROR'.  The first is always recoverable,
  2916.      and serves only to indicate that something is not quite right, and
  2917.      so may be useful in determining where something started to go
  2918.      wrong.  The second may or may not be recoverable, and the library
  2919.      terminates the program if it is fatal, displaying any relevant
  2920.      information as it does this.
  2921.  
  2922.   2. Allocation function.  This is the name of the function used to
  2923.      allocate, reallocate or free memory where the error was detected.
  2924.      This may be omitted if an error is detected elsewhere in the
  2925.      library.
  2926.  
  2927.    The following information is related to the information that the
  2928. library has stored about the relevant memory allocation.  This
  2929. information is always displayed in this format when details of
  2930. individual memory allocations are required.  If any information is
  2931. missing then it simply means that the library was not able to determine
  2932. it when the memory block was first allocated.
  2933.  
  2934.   3. Address of memory allocation.
  2935.  
  2936.   4. Size of memory allocation.
  2937.  
  2938.   5. Allocation function.  This is the name of the function that was
  2939.      called to allocate the memory block, in this case `malloc'.  If
  2940.      the memory allocation has been resized then this will be either
  2941.      `realloc', `recalloc' or `expand'.
  2942.  
  2943.   6. Allocation index.
  2944.  
  2945.   7. Reallocation index.  This is used to count the number of times a
  2946.      memory allocation has been resized with `realloc()', `recalloc()'
  2947.      or `expand()'.
  2948.  
  2949.   8. Function where original call to `malloc()' took place.  If the
  2950.      memory allocation has been resized then this will be the name of
  2951.      the function which last called `realloc()', `recalloc()' or
  2952.      `expand()'.
  2953.  
  2954.   9. Filename in which original call to `malloc()' took place.  If the
  2955.      memory allocation has been resized then this will be the filename
  2956.      in which the last call to `realloc()', `recalloc()' or `expand()'
  2957.      took place.
  2958.  
  2959.  10. Line number at which original call to `malloc()' took place.  If
  2960.      the memory allocation has been resized then this will be the line
  2961.      number at which the last call to `realloc()', `recalloc()' or
  2962.      `expand()' took place.
  2963.  
  2964.  11. Function call stack of original memory allocation.  If the memory
  2965.      allocation has been resized then this will be the call stack of
  2966.      the last call to `realloc()', `recalloc()' or `expand()'.
  2967.  
  2968.    So, the mpatrol library detected the error in the above program and
  2969. terminated it.  When the library terminates it always displays a
  2970. summary of various memory allocation statistics and settings that were
  2971. used during the execution of the program.
  2972.  
  2973.    The various settings and statistics displayed by the library for the
  2974. above example have been numbered and their descriptions appear below.
  2975.  
  2976.       1  system page size:  8192 bytes
  2977.       2  default alignment: 8 bytes
  2978.       3  overflow size:     0 bytes
  2979.       4  overflow byte:     0xAA
  2980.       5  allocation byte:   0xFF
  2981.       6  free byte:         0x55
  2982.       7  allocation stop:   0
  2983.       8  reallocation stop: 0
  2984.       9  free stop:         0
  2985.      10  unfreed abort:     0
  2986.      11  small boundary:    32
  2987.      12  medium boundary:   256
  2988.      13  large boundary:    2048
  2989.      14  lower check range: -
  2990.      15  upper check range: -
  2991.      16  failure frequency: 0
  2992.      17  failure seed:      533453
  2993.      18  prologue function: <unset>
  2994.      19  epilogue function: <unset>
  2995.      20  handler function:  <unset>
  2996.      21  log file:          mpatrol.log
  2997.      22  profiling file:    mpatrol.out
  2998.      23  program filename:  ./test1
  2999.      24  symbols read:      3240
  3000.      25  autosave count:    0
  3001.      26  allocation count:  13
  3002.      27  allocation peak:   4720 bytes
  3003.      28  allocation limit:  0 bytes
  3004.      29  allocated blocks:  1 (16 bytes)
  3005.      30  freed blocks:      0 (0 bytes)
  3006.      31  free blocks:       1 (8176 bytes)
  3007.      32  internal blocks:   25 (204800 bytes)
  3008.      33  total heap usage:  212992 bytes
  3009.      34  total compared:    0 bytes
  3010.      35  total located:     0 bytes
  3011.      36  total copied:      0 bytes
  3012.      37  total set:         0 bytes
  3013.      38  total warnings:    0
  3014.      39  total errors:      1
  3015.  
  3016.   1. System page size.  This value is used on some platforms when
  3017.      allocating and protecting system memory.
  3018.  
  3019.   2. Default alignment.  This value is the minimum alignment required
  3020.      for general purpose memory allocations, and is usually the
  3021.      alignment required by the most restrictive datatype on a given
  3022.      system.  It is used when allocating memory that has no specified
  3023.      alignment.  It can be changed at run-time using the `DEFALIGN'
  3024.      option, but setting this value too small may cause the program to
  3025.      crash due to bus errors which are caused by reading from or
  3026.      writing to misaligned data.
  3027.  
  3028.   3. Overflow size.  This value is the size used by one overflow
  3029.      buffer.  If this is non-zero then every memory allocation will
  3030.      have two overflow buffers; one on either side.  These buffers are
  3031.      used by the library to detect if the program has written too many
  3032.      bytes to a memory allocation, thus overflowing into one of the
  3033.      buffers, but these extra checks can slow down execution speed.  It
  3034.      can be changed at run-time using the `OFLOWSIZE' option.
  3035.  
  3036.   4. Overflow byte.
  3037.  
  3038.   5. Allocation byte.
  3039.  
  3040.   6. Free byte.  These values are used by the library to pre-fill
  3041.      blocks of memory for checking purposes.  The overflow byte is used
  3042.      to fill overflow buffers, the allocation byte is used to fill
  3043.      newly-allocated memory (except from `calloc()' or `recalloc()'),
  3044.      and the free byte is used to fill free blocks or freed memory
  3045.      allocations.  These can be changed at run-time using the
  3046.      `OFLOWBYTE', `ALLOCBYTE' and `FREEBYTE' options.
  3047.  
  3048.   7. Allocation stop.
  3049.  
  3050.   8. Reallocation stop.
  3051.  
  3052.   9. Free stop.  These values are used by the library to halt the
  3053.      program when run inside a debugger whenever a specified allocation
  3054.      index is allocated, reallocated or freed.  These can be changed at
  3055.      run-time using the `ALLOCSTOP', `REALLOCSTOP' and `FREESTOP'
  3056.      options.
  3057.  
  3058.  10. Unfreed abort.  This value is used when the program terminates and
  3059.      is used by the library to check if there are more than a given
  3060.      number of unfreed memory allocations.  If there are then the
  3061.      library will cause the program to abort with an error.  It can be
  3062.      changed at run-time using the `UNFREEDABORT' option.
  3063.  
  3064.  11. Small boundary.
  3065.  
  3066.  12. Medium boundary.
  3067.  
  3068.  13. Large boundary.  These values are used in memory allocation
  3069.      profiling and specify the boundaries in bytes between small,
  3070.      medium, large and extra large allocations.  These can be changed
  3071.      at run-time using the `SMALLBOUND', `MEDIUMBOUND' and `LARGEBOUND'
  3072.      options.
  3073.  
  3074.  14. Lower check range.
  3075.  
  3076.  15. Upper check range.  These values specify the range of allocation
  3077.      indices through which the library will physically check every area
  3078.      of free memory and every overflow buffer for errors.  A dash
  3079.      specifies that either the lower or upper range is infinite, but if
  3080.      they are both zero then no such checking will ever be performed,
  3081.      thus speeding up execution speed dramatically.  The library
  3082.      defaults to performing checks for every allocation index.  These
  3083.      can be changed at run-time using the `CHECK' option.
  3084.  
  3085.  16. Failure frequency.
  3086.  
  3087.  17. Failure seed.  These values are used to specify if random memory
  3088.      allocation failures should occur during program execution, for the
  3089.      purposes of stress testing a program.  If the failure frequency is
  3090.      zero then no random failures will occur, but if it is greater than
  3091.      zero then the higher the number, the less frequent the failures.
  3092.      The failure seed is used internally by the mpatrol library when
  3093.      generating random numbers.  If it is zero then the seed will be set
  3094.      randomly, but if it is greater than zero then it will be used to
  3095.      generate a predictable sequence of random numbers; i.e. two runs
  3096.      of the same program with the same failure frequencies and the same
  3097.      failure seeds will generate exactly the same sequence of failures.
  3098.  
  3099.  18. Prologue function.
  3100.  
  3101.  19. Epilogue function.
  3102.  
  3103.  20. Handler function.  These values contain addresses or names of
  3104.      functions that have been installed as callback functions for the
  3105.      library.  These functions, if set, will be called from the library
  3106.      at appropriate times during program execution in order to handle
  3107.      specific events.  These can be changed at compile-time using the
  3108.      `__mp_prologue()', `__mp_epilogue()' and `__mp_nomemory()'
  3109.      functions.
  3110.  
  3111.  21. Log file.  Simply contains the name of the file where all mpatrol
  3112.      library diagnostics go to.  It can be changed at run-time using
  3113.      the `LOGFILE' option.
  3114.  
  3115.  22. Profiling file.  Contains the name of the file where all of the
  3116.      mpatrol library memory allocation profiling information goes when
  3117.      the `PROF' option is used.  It can be changed at run-time using
  3118.      the `PROFFILE' option.
  3119.  
  3120.  23. Program filename.  Contains the full pathname to the program's
  3121.      executable file.  This is used by the mpatrol library to read the
  3122.      symbol table in order to provide symbolic information in function
  3123.      call stacks.  It can be changed at run-time using the `PROGFILE'
  3124.      option.
  3125.  
  3126.  24. Symbols read.  This value contains the total number of symbols
  3127.      read from a program's executable file and/or the dynamic linker,
  3128.      if applicable.
  3129.  
  3130.  25. Autosave count.  This value contains the frequency at which the
  3131.      mpatrol library should periodically write the profiling data to
  3132.      the profiling output file.  When the total number of profiled
  3133.      memory allocations and deallocations is a multiple of this number
  3134.      then the current profiling information will be written to the
  3135.      profiling output file.  It can be changed at run-time using the
  3136.      `AUTOSAVE' option.
  3137.  
  3138.  26. Allocation count.  This value contains the total number of memory
  3139.      allocations that were created by the mpatrol library.  This value
  3140.      may be more than expected if the mpatrol library makes any memory
  3141.      allocations during initialisation.
  3142.  
  3143.  27. Allocation peak.  This value contains the peak memory usage set by
  3144.      the program when running.  This value may be more than expected if
  3145.      the mpatrol library makes any memory allocations during
  3146.      initialisation.
  3147.  
  3148.  28. Allocation limit.  This value is used to limit the amount of
  3149.      memory that can be allocated by a program, which can be useful for
  3150.      stress-testing in simulated low memory conditions.  It can be
  3151.      changed at run-time using the `LIMIT' option.
  3152.  
  3153.  29. Allocated blocks.
  3154.  
  3155.  30. Freed blocks.
  3156.  
  3157.  31. Free blocks.  These values contain the total number of allocated,
  3158.      freed and free blocks at the time the summary was produced.  A
  3159.      freed block is an allocated block that has been freed but has not
  3160.      been returned to the free memory list for later allocation.  These
  3161.      values may be different from those expected if the mpatrol library
  3162.      makes any memory allocations during initialisation.
  3163.  
  3164.  32. Internal blocks.  This value contains the total number of memory
  3165.      blocks (of varying sizes) that have been allocated from the system
  3166.      for the mpatrol library to use internally.  These memory blocks
  3167.      will be write-protected on systems that support memory protection
  3168.      in order to prevent the program from corrupting the library's data
  3169.      structures.  This can be overridden at run-time using the
  3170.      `NOPROTECT' option in order to speed up program execution slightly.
  3171.  
  3172.  33. Total heap usage.  This value contains the total amount of system
  3173.      heap memory that has been allocated by the mpatrol library.
  3174.  
  3175.  34. Total compared.
  3176.  
  3177.  35. Total located.
  3178.  
  3179.  36. Total copied.
  3180.  
  3181.  37. Total set.  These values contain the total number of bytes that
  3182.      have been tracked by the mpatrol library in byte comparison
  3183.      operations (such as `memcmp()'), byte location operations (such as
  3184.      `memchr()', byte copy operations (such as `memcpy()') and byte set
  3185.      operations (such as `memset()') respectively.  They do not take
  3186.      into account any other such operations that occur outwith these
  3187.      functions, such as loading and storing from machine instructions.
  3188.  
  3189.  38. Total warnings.
  3190.  
  3191.  39. Total errors.  The library keeps a count of the total number of
  3192.      warnings and errors it has displayed so that you can quickly work
  3193.      out this information at program termination.
  3194.  
  3195.    ---------- Footnotes ----------
  3196.  
  3197.    (1) This information may also be filled in if the `USEDEBUG' option
  3198. is used and supported, and if debugging information about the call to
  3199. `malloc()' is available.
  3200.  
  3201. 
  3202. File: mpatrol.info,  Node: Example 2,  Next: Example 3,  Prev: Example 1,  Up: Examples
  3203.  
  3204. Detecting incorrect reuse of freed memory
  3205. =========================================
  3206.  
  3207.    The next example uses `tests/fail/test2.c' to illustrate how the
  3208. mpatrol library can detect whereabouts on the heap an address belongs.
  3209.  
  3210.      23  /*
  3211.      24   * Allocates a block of 16 bytes and then immediately frees it.  An
  3212.      25   * attempt is then made to double the size of the original block.
  3213.      26   */
  3214.      
  3215.      
  3216.      29  #include "mpatrol.h"
  3217.      
  3218.      
  3219.      32  int main(void)
  3220.      33  {
  3221.      34      char *p;
  3222.      
  3223.      36      if (p = (char *) malloc(16))
  3224.      37      {
  3225.      38          free(p);
  3226.      39          p = (char *) realloc(p, 32);
  3227.      40      }
  3228.      41      return EXIT_SUCCESS;
  3229.      42  }
  3230.  
  3231.    The relevant excerpts from `mpatrol.log' appear below.  The format
  3232. of the log messages should be familiar to you now.
  3233.  
  3234.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test2.c|36]
  3235.              0x00010B18 main
  3236.              0x00010A0C _start
  3237.      
  3238.      returns 0x00028000
  3239.      
  3240.      FREE: free (0x00028000) [main|test2.c|38]
  3241.              0x00010B54 main
  3242.              0x00010A0C _start
  3243.      
  3244.          0x00028000 (16 bytes) {malloc:13:0} [main|test2.c|36]
  3245.              0x00010B18 main
  3246.              0x00010A0C _start
  3247.      
  3248.      REALLOC: realloc (0x00028000, 32 bytes, 8 bytes) [main|test2.c|39]
  3249.              0x00010B88 main
  3250.              0x00010A0C _start
  3251.      
  3252.      ERROR: realloc: 0x00028000 has not been allocated
  3253.      
  3254.      returns 0x00000000
  3255.  
  3256.    The mpatrol library stores all of its information about allocated
  3257. and free memory in tree structures so that it can quickly determine if
  3258. an address belongs to allocated or free memory, or if it even exists in
  3259. the heap that is managed by mpatrol.  The above example should
  3260. illustrate this since after the allocation had been freed, the library
  3261. recognised this and reported an error.  It was possible for the program
  3262. to continue execution even after that error since mpatrol could recover
  3263. from it and return `NULL'.
  3264.  
  3265.    It is possible for mpatrol to give even more useful diagnostics in
  3266. the above situation by using the `NOFREE' option.  This prevents the
  3267. library from returning any freed allocations to the free memory pool,
  3268. by preserving any information about them and marking them as freed.  If
  3269. you add the `NOFREE' option to the `MPATROL_OPTIONS' environment
  3270. variable you should see the following entries in `mpatrol.log' instead.
  3271.  
  3272.      ALLOC: malloc (13, 16 bytes, 8 bytes) [main|test2.c|36]
  3273.              0x00010B18 main
  3274.              0x00010A0C _start
  3275.      
  3276.      returns 0x00029DE0
  3277.      
  3278.      FREE: free (0x00029DE0) [main|test2.c|38]
  3279.              0x00010B54 main
  3280.              0x00010A0C _start
  3281.      
  3282.          0x00029DE0 (16 bytes) {malloc:13:0} [main|test2.c|36]
  3283.              0x00010B18 main
  3284.              0x00010A0C _start
  3285.      
  3286.      REALLOC: realloc (0x00029DE0, 32 bytes, 8 bytes) [main|test2.c|39]
  3287.              0x00010B88 main
  3288.              0x00010A0C _start
  3289.      
  3290.      ERROR: realloc: 0x00029DE0 was freed with free
  3291.          0x00029DE0 (16 bytes) {free:13:0} [main|test2.c|38]
  3292.              0x00010B54 main
  3293.              0x00010A0C _start
  3294.      
  3295.      returns 0x00000000
  3296.  
  3297.    Note the extra information reported by `realloc()' since the library
  3298. knows all of the details about the freed memory allocation and when it
  3299. was freed.
  3300.  
  3301.    The `NOFREE' option tends to use up much more system memory than
  3302. normal since it effectively instructs the mpatrol library to allocate
  3303. new memory for every single memory allocation or reallocation.  It can
  3304. also slow down program execution when overflow buffers are used, since
  3305. with each new memory allocation the library needs to check more and
  3306. more overflow buffers every time it is called.  However, it can be
  3307. quite useful for problems such as this one.  The test in
  3308. `tests/fail/test3.c' has a similar situation.
  3309.  
  3310.    Normally, the `NOFREE' option will cause the library to fill all
  3311. freed memory allocations with the free byte.  However, the original
  3312. contents of such allocations can be preserved with the `PRESERVE'
  3313. option.  This could help in situations when you need to determine
  3314. exactly if a program is relying on the contents of freed memory.
  3315.  
  3316. 
  3317. File: mpatrol.info,  Node: Example 3,  Next: Example 4,  Prev: Example 2,  Up: Examples
  3318.  
  3319. Detecting use of free memory
  3320. ============================
  3321.  
  3322.    This next example illustrates how the mpatrol library is able to
  3323. check to see if anything has been written into free memory.  The test
  3324. is located in `tests/fail/test4.c' and simply writes a single byte into
  3325. free memory.
  3326.  
  3327.      23  /*
  3328.      24   * Allocates a block of 16 bytes and then immediately frees it.  A
  3329.      25   * NULL character is written into the middle of the freed memory.
  3330.      26   */
  3331.      
  3332.      
  3333.      29  #include "mpatrol.h"
  3334.      
  3335.      
  3336.      32  int main(void)
  3337.      33  {
  3338.      34      char *p;
  3339.      
  3340.      36      if (p = (char *) malloc(16))
  3341.      37      {
  3342.      38          free(p);
  3343.      39          p[8] = '\0';
  3344.      40      }
  3345.      41      return EXIT_SUCCESS;
  3346.      42  }
  3347.  
  3348.    The following output was produced as part of `mpatrol.log'.  Note
  3349. that this test was run using the same `MPATROL_OPTIONS' settings as the
  3350. last example, but make sure that `PRESERVE' is not set.
  3351.  
  3352.      ERROR: freed allocation 0x00029DE0 has memory corruption at 0x00029DE8
  3353.              0x00029DE8  00555555 55555555                    .UUUUUUU
  3354.      
  3355.          0x00029DE0 (16 bytes) {free:13:0} [main|test4.c|38]
  3356.              0x00010B1C main
  3357.              0x000109D4 _start
  3358.  
  3359.    The library was able to detect that something had been written into
  3360. free memory and could report on the memory allocation that was
  3361. overwritten.  However, these checks are only performed whenever a
  3362. function in the mpatrol library is called.  In the example above, the
  3363. code which wrote into free memory could have been miles away from where
  3364. the library detected the error.
  3365.  
  3366.    On platforms that support memory protection, the library also
  3367. supports the `PAGEALLOC' option.  This option instructs the library to
  3368. force every single memory allocation to have a size which is a multiple
  3369. of the system page size.  Although the library still stores the
  3370. original requested size, it effectively means that no two memory
  3371. allocations occupy the same page of memory.  It can then use page
  3372. protection (which only operates on pages of memory) to protect all free
  3373. memory from being read from or written to, and uses similar features to
  3374. install a page of overflow buffer on either side of the allocation.
  3375.  
  3376.    However, if the requested size for the memory allocation was not a
  3377. multiple of the page size this means that there will still be unused
  3378. space left over in the allocated pages.  This problem is solved by
  3379. turning the unused space into overflow buffers that will be checked in
  3380. the normal way.  The positioning of the allocation within its pages is
  3381. also important.  If you want to check for illegal reads from the
  3382. borders of the memory allocation, unless it fits exactly into its pages
  3383. then there is a chance that a program could illegally read the
  3384. right-most overflow buffer if the allocation was left-aligned, or
  3385. vice-versa.  Two settings therefore exist for the `PAGEALLOC' option:
  3386. `LOWER' and `UPPER'.  They refer to the placement of every memory
  3387. allocation within its constituent pages.
  3388.  
  3389.    The following diagram illustrates the `PAGEALLOC' option.  In the
  3390. diagram, the system page size is assumed to be 16 bytes (very unlikely,
  3391. but will serve for this example) and each character represents 1 byte.
  3392.  
  3393.      x = allocated memory
  3394.      o = overflow buffer (filled with the overflow byte)
  3395.      . = overflow buffer page (read and write protected)
  3396.      
  3397.      PAGEALLOC=LOWER, allocation size is 16 bytes or
  3398.      PAGEALLOC=UPPER, allocation size is 16 bytes:
  3399.          ................xxxxxxxxxxxxxxxx................
  3400.      
  3401.      PAGEALLOC=LOWER, allocation size is 8 bytes:
  3402.          ................xxxxxxxxoooooooo................
  3403.      
  3404.      PAGEALLOC=UPPER, allocation size is 8 bytes:
  3405.          ................ooooooooxxxxxxxx................
  3406.  
  3407.    In our original example, if the `PAGEALLOC=LOWER' option is added to
  3408. the `MPATROL_OPTIONS' environment variable then the following error
  3409. will be produced instead of the original error.
  3410.  
  3411.      ERROR: illegal memory access at address 0x0009E008
  3412.          0x0009E000 (16 bytes) {free:13:0} [main|test4.c|38]
  3413.              0x00010B1C main
  3414.              0x000109D4 _start
  3415.      
  3416.          call stack
  3417.              0x00010B1C main
  3418.              0x000109D4 _start
  3419.  
  3420.    On systems that support memory protection, the mpatrol library has a
  3421. built-in signal handler which catches illegal memory accesses and
  3422. terminates the program.  In the above case, the freed memory was made
  3423. write-protected and so could not be written to.  The underlying virtual
  3424. memory system in the operating system noticed this and signaled this to
  3425. the library immediately after it happened.
  3426.  
  3427.    Along with the details of the freed memory allocation that was being
  3428. written to, the library also attempts to display the function call
  3429. stack for the location in the program that caused the illegal memory
  3430. access, although this can be quite unreliable.  A better solution would
  3431. be to run the program in a debugger to catch the illegal memory access.
  3432.  
  3433.    Note that the `PAGEALLOC' option also modifies the behaviour of the
  3434. `NOFREE' and `PRESERVE' options when used together.  The memory
  3435. allocation being freed will always be made write-protected when the
  3436. `PRESERVE' option is used, otherwise it will also be made read-protected
  3437. to prevent further accesses.
  3438.  
  3439.    Note also that the `PAGEALLOC=UPPER' option is potentially much less
  3440. efficient at catching illegal memory accesses than the `PAGEALLOC=LOWER'
  3441. option.  This is due to alignment requirements, since an allocation of
  3442. 1 byte requiring an alignment of 16 bytes cannot be placed at the very
  3443. end of a page of size 4096 bytes.  The following diagram illustrates
  3444. this, using the same page size as the last diagram.
  3445.  
  3446.      x = allocated memory
  3447.      o = overflow buffer (filled with the overflow byte)
  3448.      . = overflow buffer page (read and write protected)
  3449.      
  3450.      PAGEALLOC=UPPER, allocation size is 16 bytes, alignment is 8 bytes:
  3451.          ................xxxxxxxxxxxxxxxx................
  3452.      
  3453.      PAGEALLOC=UPPER, allocation size is 3 bytes, alignment is 1 byte:
  3454.          ................oooooooooooooxxx................
  3455.      
  3456.      PAGEALLOC=UPPER, allocation size is 3 bytes, alignment is 8 bytes:
  3457.          ................ooooooooxxxooooo................
  3458.  
  3459.    Everything is OK until the last allocation, where the alignment
  3460. requirement means that there must be two overflow buffers.  This slows
  3461. down program execution since the library must check an additional
  3462. overflow buffer, and also means that the program would have to read six
  3463. bytes beyond the end of the allocation before the illegal memory access
  3464. would be detected.
  3465.  
  3466. 
  3467. File: mpatrol.info,  Node: Example 4,  Next: Example 5,  Prev: Example 3,  Up: Examples
  3468.  
  3469. Using overflow buffers
  3470. ======================
  3471.  
  3472.    This example illustrates the use of overflow buffers and so the
  3473. `MPATROL_OPTIONS' environment variable should have `OFLOWSIZE=2' added
  3474. to it.  However, turn off any `PAGEALLOC' options for the purposes of
  3475. this example.  The test is located in `tests/fail/test5.c', and
  3476. `tests/fail/test6.c' is very similar.
  3477.  
  3478.      23  /*
  3479.      24   * Allocates a block of 16 bytes and then copies a string of 16
  3480.      25   * bytes into the block.  However, the string is copied to 1 byte
  3481.      26   * before the allocated block which writes before the start of the
  3482.      27   * block.  This test must be run with an OFLOWSIZE greater than 0.
  3483.      28   */
  3484.      
  3485.      
  3486.      31  #include "mpatrol.h"
  3487.      
  3488.      
  3489.      34  int main(void)
  3490.      35  {
  3491.      36      char *p;
  3492.      
  3493.      38      if (p = (char *) malloc(16))
  3494.      39      {
  3495.      40          strcpy(p - 1, "this test fails!");
  3496.      41          free(p);
  3497.      42      }
  3498.      43      return EXIT_SUCCESS;
  3499.      44  }
  3500.  
  3501.    The following error should be produced in `mpatrol.log'.
  3502.  
  3503.      ERROR: allocation 0x00029E28 has a corrupted overflow buffer at 0x00029E27
  3504.              0x00029E26  AA74                                 ªt
  3505.      
  3506.          0x00029E28 (16 bytes) {malloc:13:0} [main|test5.c|38]
  3507.              0x00010B0C main
  3508.              0x00010A00 _start
  3509.  
  3510.    Once again, the library attempts to show you as much detail as
  3511. possible about where the corruption occurred.  Along with showing you a
  3512. memory dump of the overflow buffer that was corrupted, it also shows
  3513. you the allocation to which the overflow buffer belongs.
  3514.  
  3515.    Using overflow buffers can reduce the speed of program execution
  3516. since the library has to check every buffer whenever it is called, and
  3517. if the buffers are larger then they'll take longer to check and will
  3518. use up more memory.  However, larger buffers mean that there is less
  3519. chance of the program writing past one memory allocation into another.
  3520.  
  3521.    Alternatively, the `CHECK' option can be used to limit the number of
  3522. checks that the library has to perform, thus speeding up program
  3523. execution.  This option specifies a range of allocation indices through
  3524. which the library will check overflow buffers and free memory for
  3525. corruption.  Such checks occur when they normally would, but only if
  3526. the current allocation index falls within the specified range.  This
  3527. feature can be used when there is a suspicion that free memory
  3528. corruption or overflow buffer corruption occurs at a certain point
  3529. during program execution, but checking them at every library call would
  3530. take too long.
  3531.  
  3532.    On systems which support software watch points, there is an extra
  3533. option called `OFLOWWATCH' which allows additional memory protection.
  3534. Watch points allow individual bytes to be read and/or write protected
  3535. as opposed to just pages.  The `OFLOWWATCH' option installs software
  3536. watch points at every overflow buffer instead of requiring the library
  3537. to check the integrity of the overflow buffers, and can be used in
  3538. combination with `PAGEALLOC'.  However, software watch points slow down
  3539. program execution to a crawl since every machine instruction must be
  3540. checked individually by the system to see if it accesses a watch point
  3541. area.  Slowing the program down by a factor of 10,000 is not uncommon
  3542. on some systems when the `OFLOWWATCH' option is used.
  3543.  
  3544. 
  3545. File: mpatrol.info,  Node: Example 5,  Next: Example 6,  Prev: Example 4,  Up: Examples
  3546.  
  3547. Bad memory operations
  3548. =====================
  3549.  
  3550.    In C there are several basic memory operation functions that are
  3551. often called to perform tasks such as clearing memory, copying memory,
  3552. etc.  The mpatrol library contains replacements for these which allow
  3553. for better checking of their arguments to prevent reading and writing
  3554. past the boundaries of existing memory allocations.  The following
  3555. source can be found in `tests/fail/test9.c'.
  3556.  
  3557.      23  /*
  3558.      24   * Allocates a block of 16 bytes and then attempts to zero the contents of
  3559.      25   * the block.  However, a zero byte is also written 1 byte before and 1
  3560.      26   * byte after the allocated block, resulting in an error in the log file.
  3561.      27   */
  3562.      
  3563.      
  3564.      30  #include "mpatrol.h"
  3565.      
  3566.      
  3567.      33  int main(void)
  3568.      34  {
  3569.      35      char *p;
  3570.      
  3571.      37      if (p = (char *) malloc(16))
  3572.      38      {
  3573.      39          memset(p - 1, 0, 18);
  3574.      40          free(p);
  3575.      41      }
  3576.      42      return EXIT_SUCCESS;
  3577.      43  }
  3578.  
  3579.    When this is compiled and run, the following should appear in the
  3580. log file.
  3581.  
  3582.      ERROR: memset: range [0x00027FFF,0x00028010] overflows [0x00028000,0x0002800F]
  3583.          0x00028000 (16 bytes) {malloc:13:0} [main|test9.c|37]
  3584.              0x00010B18 main
  3585.              0x00010A0C _start
  3586.  
  3587.    As you can see, the library detected that the `memset()' function
  3588. would have written past the boundaries of the memory allocation and
  3589. reported this to you.  It then proceeded to ignore the request to copy
  3590. the memory and continued with the execution of the program(1).  Note
  3591. that this will only be done for known memory allocations.  Reading and
  3592. writing past the boundaries of static and stack memory allocations
  3593. cannot be detected in this way.
  3594.  
  3595.    If the `LOGMEMORY' option is added to the `MPATROL_OPTIONS'
  3596. environment variable then it is possible to see a log of all the
  3597. mpatrol library memory operation functions that were called during
  3598. program execution.  For example, adding this option and running the
  3599. above program again will produce something similar to the following.
  3600.  
  3601.      MEMSET: memset (0x00027FFF, 18 bytes, 0x00) [main|test9.c|39]
  3602.              0x00010B18 main
  3603.              0x00010A0C _start
  3604.  
  3605.    This is similar to the tracing produced for memory allocation
  3606. functions, except that the arguments in parentheses mean different
  3607. things.  For `MEMSET', the first argument represents the start of the
  3608. memory block to set, the second argument represents the number of bytes
  3609. to set and the third argument represents the actual byte to set.
  3610.  
  3611.    For `MEMCOPY', the first argument represents the source memory
  3612. block, the second argument represents the destination memory block, the
  3613. third argument represents the number of bytes to copy and the fourth
  3614. argument represents a byte to copy up to if `memccpy()' is being
  3615. called.  This is similar for `MEMCMP'.
  3616.  
  3617.    For `MEMFIND', the first and second arguments represent the source
  3618. memory block and its length, while the third and fourth arguments
  3619. represent the memory block to search for and its length.  In the
  3620. implementation for `memchr()', the byte to search for is copied to a
  3621. one byte buffer and the address of that buffer is used as the memory
  3622. block to search for.
  3623.  
  3624.    Note that as with the memory allocation functions, `MEMCMP',
  3625. `MEMFIND', `MEMCOPY' and `MEMSET' are used to generalise the types of
  3626. operations being performed and are followed by the names of the actual
  3627. functions being used.  In some cases the functions may use a different
  3628. ordering of parameters than that shown.
  3629.  
  3630.    ---------- Footnotes ----------
  3631.  
  3632.    (1) The error can be turned into a warning with the `ALLOWOFLOW'
  3633. option which will also force the operation to be performed.
  3634.  
  3635. 
  3636. File: mpatrol.info,  Node: Example 6,  Next: Example 7,  Prev: Example 5,  Up: Examples
  3637.  
  3638. Incompatible function calls
  3639. ===========================
  3640.  
  3641.    This example illustrates how the mpatrol library checks for calls to
  3642. incompatible pairs of memory allocation functions.  It requires the use
  3643. of C++, although does not use any C++ features except for overloaded
  3644. operators.  The source is in `tests/fail/test7.c', and
  3645. `tests/fail/test8.c' is similar.
  3646.  
  3647.      23  /*
  3648.      24   * Allocates a block of 16 bytes using C++ operator new[] and then
  3649.      25   * attempts to free it using C++ operator delete.
  3650.      26   */
  3651.      
  3652.      
  3653.      29  #include "mpatrol.h"
  3654.      
  3655.      
  3656.      32  int main(void)
  3657.      33  {
  3658.      34      char *p;
  3659.      
  3660.      36      p = new char[16];
  3661.      37      delete p;
  3662.      38      return EXIT_SUCCESS;
  3663.      39  }
  3664.  
  3665.    The relevant parts of `mpatrol.log' are shown below.
  3666.  
  3667.      ALLOC: operator new[] (17, 16 bytes, 8 bytes) [int main()|test7.c|36]
  3668.              0x00010A28 __builtin_vec_new
  3669.              0x00010ADC main
  3670.              0x000108D0 _start
  3671.      
  3672.      returns 0x00028000
  3673.      
  3674.      FREE: operator delete (0x00028000) [int main()|test7.c|37]
  3675.              0x00010A74 __builtin_delete
  3676.              0x00010AF0 main
  3677.              0x000108D0 _start
  3678.      
  3679.      ERROR: operator delete: 0x00028000 was allocated with operator new[]
  3680.          0x00028000 (16 bytes) {operator new[]:17:0} [int main()|test7.c|36]
  3681.              0x00010A28 __builtin_vec_new
  3682.              0x00010ADC main
  3683.              0x000108D0 _start
  3684.  
  3685.    This shows a call to `operator new[]', closely followed by a call to
  3686. `operator delete'.  However, in C++ calls to `operator new[]' must be
  3687. matched by calls to `operator delete[]' and not `operator delete'.
  3688. Hence, the library reports this as an error and does not free the memory
  3689. allocation.
  3690.  
  3691. 
  3692. File: mpatrol.info,  Node: Example 7,  Prev: Example 6,  Up: Examples
  3693.  
  3694. Additional useful information
  3695. =============================
  3696.  
  3697.    This last example illustrates the various `SHOW' options that are
  3698. available for displaying additional information from the mpatrol
  3699. library at program termination.  It also shows how to easily detect
  3700. memory leaks.  Use the `OFLOWSIZE=16', `NOFREE' and `SHOWALL' options in
  3701. `MPATROL_OPTIONS' before running.
  3702.  
  3703.       1  /*
  3704.       2   * Introduces a memory leak by clobbering a pointer with a new
  3705.       3   * memory allocation.  Use with SHOWUNFREED to display it.
  3706.       4   */
  3707.      
  3708.      
  3709.       7  #include "mpatrol.h"
  3710.      
  3711.      
  3712.      10  int main(void)
  3713.      11  {
  3714.      12      void *p;
  3715.      
  3716.      14      p = malloc(4);
  3717.      15      p = malloc(4);
  3718.      16      if (p != NULL)
  3719.      17          free(p);
  3720.      18      return EXIT_SUCCESS;
  3721.      19  }
  3722.  
  3723.    The information that we are interested in comes after the summary of
  3724. library statistics generated in the log file.  The first block of data
  3725. shows a memory map of the heap that is being handled by mpatrol.  This
  3726. can be used to see graphically where a particular allocation is
  3727. located, or to look for memory fragmentation.  The `SHOWMAP' option
  3728. also displays this information.
  3729.  
  3730.    Note that gaps in the memory map can either be due to space used by
  3731. internal memory blocks or to some other memory allocation library using
  3732. up space.  On some systems that don't have virtual memory, gaps are
  3733. likely to be owned by other processes or belong to the system free
  3734. memory list.
  3735.  
  3736.      memory map:
  3737.        / 0x8000A000-0x8000A00F overflow (16 bytes)
  3738.       |+ 0x8000A010-0x8000A077 allocated (104 bytes) {malloc:1:0} [-|-|-]
  3739.        \ 0x8000A078-0x8000A087 overflow (16 bytes)
  3740.        / 0x8000A088-0x8000A097 overflow (16 bytes)
  3741.       |+ 0x8000A098-0x8000A115 freed (126 bytes) {free:2:0} [-|-|-]
  3742.        \ 0x8000A116-0x8000A125 overflow (16 bytes)
  3743.        / 0x8000A126-0x8000A135 overflow (16 bytes)
  3744.       |+ 0x8000A136-0x8000AF05 freed (3536 bytes) {free:3:0} [-|-|-]
  3745.        \ 0x8000AF06-0x8000AF15 overflow (16 bytes)
  3746.        / 0x8000AF16-0x8000AF25 overflow (16 bytes)
  3747.       |+ 0x8000AF26-0x8000AFA3 freed (126 bytes) {free:4:0} [-|-|-]
  3748.        \ 0x8000AFA4-0x8000AFB3 overflow (16 bytes)
  3749.        / 0x8000AFB4-0x8000AFC3 overflow (16 bytes)
  3750.       |+ 0x8000AFC4-0x8000AFC7 allocated (4 bytes) {malloc:10:0} [main|test.c|14]
  3751.        \ 0x8000AFC8-0x8000AFD7 overflow (16 bytes)
  3752.        / 0x8000AFD8-0x8000AFE7 overflow (16 bytes)
  3753.       |+ 0x8000AFE8-0x8000AFEB freed (4 bytes) {free:11:0} [main|test.c|17]
  3754.        \ 0x8000AFEC-0x8000AFFB overflow (16 bytes)
  3755.      --- 0x8000AFFC-0x8000AFFF free (4 bytes)
  3756.          --------------------- gap (12288 bytes)
  3757.        / 0x8000E000-0x8000E00F overflow (16 bytes)
  3758.       |+ 0x8000E010-0x8000EA27 freed (2584 bytes) {free:5:0} [-|-|-]
  3759.        \ 0x8000EA28-0x8000EA37 overflow (16 bytes)
  3760.        / 0x8000EA38-0x8000EA47 overflow (16 bytes)
  3761.       |+ 0x8000EA48-0x8000EAC5 freed (126 bytes) {free:6:0} [-|-|-]
  3762.        \ 0x8000EAC6-0x8000EAD5 overflow (16 bytes)
  3763.        / 0x8000EAD6-0x8000EAE5 overflow (16 bytes)
  3764.       |+ 0x8000EAE6-0x8000EB63 freed (126 bytes) {free:8:0} [-|-|-]
  3765.        \ 0x8000EB64-0x8000EB73 overflow (16 bytes)
  3766.      --- 0x8000EB74-0x8000EFFF free (1164 bytes)
  3767.          --------------------- gap (8192 bytes)
  3768.        / 0x80011000-0x8001100F overflow (16 bytes)
  3769.       |+ 0x80011010-0x800127F7 freed (6120 bytes) {free:7:0} [-|-|-]
  3770.        \ 0x800127F8-0x80012807 overflow (16 bytes)
  3771.      --- 0x80012808-0x80012FFF free (2040 bytes)
  3772.          --------------------- gap (106496 bytes)
  3773.        / 0x8002D000-0x8002D00F overflow (16 bytes)
  3774.       |+ 0x8002D010-0x8002DBBF freed (2992 bytes) {free:9:0} [-|-|-]
  3775.        \ 0x8002DBC0-0x8002DBCF overflow (16 bytes)
  3776.      --- 0x8002DBD0-0x8002DFFF free (1072 bytes)
  3777.  
  3778.    The next block of data shows a summary of all the symbols that could
  3779. be read from the program's executable file and/or any shared libraries
  3780. that the program requires.  This can be useful to see which symbols
  3781. have actually been read by the mpatrol library.  The `SHOWSYMBOLS'
  3782. option also displays this information.
  3783.  
  3784.    Note that the following data has been dramatically cut down in size
  3785. for the purposes of this example.  The `...' marks text that has been
  3786. removed.
  3787.  
  3788.      symbols read: 2438
  3789.          0x8000076C-0x800007D9 _init [./a.out] (110 bytes)
  3790.          0x80000900-0x8000094F _start [./a.out] (80 bytes)
  3791.          0x80000950-0x8000096F __do_global_dtors_aux [./a.out] (32 bytes)
  3792.          0x80000970-0x80000977 fini_dummy [./a.out] (8 bytes)
  3793.          ...
  3794.          0x80003B24-0x80003B4B __clear_cache [./a.out] (40 bytes)
  3795.          0x80003B4C-0x80003B6F __do_global_ctors_aux [./a.out] (36 bytes)
  3796.          0x80003B70-0x80003B77 init_dummy [./a.out] (8 bytes)
  3797.          0x80003B78-0x80003BA9 _fini [./a.out] (50 bytes)
  3798.          0xC0002604-0xC0002609 _start [/lib/ld.so.1] (6 bytes)
  3799.          0xC000260A-0xC0002659 _dl_start_user [/lib/ld.so.1] (80 bytes)
  3800.          0xC000265A-0xC0002B1B _dl_start [/lib/ld.so.1] (1218 bytes)
  3801.                     0xC000266A here [/lib/ld.so.1] (0 bytes)
  3802.          ...
  3803.          0xC0007A78-0xC0007AB5 __libc_read [/lib/ld.so.1] (62 bytes)
  3804.                     0xC0007A78 read [/lib/ld.so.1] (0 bytes)
  3805.                     0xC0007A9A __syscall_error [/lib/ld.so.1] (0 bytes)
  3806.          0xC0007AB8-0xC0007ADF __clear_cache [/lib/ld.so.1] (40 bytes)
  3807.          0xC0013E70-0xC0013E8B __mp_newlist [/usr/lib/libmpatrol.so.1.0] (28 bytes)
  3808.          0xC0013E8C-0xC0013EB3 __mp_addhead [/usr/lib/libmpatrol.so.1.0] (40 bytes)
  3809.          0xC0013EB4-0xC0013EE7 __mp_addtail [/usr/lib/libmpatrol.so.1.0] (52 bytes)
  3810.          0xC0013EE8-0xC0013F1B __mp_prepend [/usr/lib/libmpatrol.so.1.0] (52 bytes)
  3811.          ...
  3812.          0xC001A0DC-0xC001A0FF __nw__FUi [/usr/lib/libmpatrol.so.1.0] (36 bytes)
  3813.          0xC001A100-0xC001A123 __arr_nw__FUi [/usr/lib/libmpatrol.so.1.0] (36 bytes)
  3814.          0xC001A124-0xC001A143 __dl__FPv [/usr/lib/libmpatrol.so.1.0] (32 bytes)
  3815.          0xC001A144-0xC001A163 __arr_dl__FPv [/usr/lib/libmpatrol.so.1.0] (32 bytes)
  3816.          0xC003BB14-0xC003BB45 __libc_global_ctors [/lib/libc.so.6] (50 bytes)
  3817.          0xC003BB48-0xC003BB97 __libc_init [/lib/libc.so.6] (80 bytes)
  3818.          0xC003BB98-0xC003BBC3 __libc_print_version [/lib/libc.so.6] (44 bytes)
  3819.          0xC003BBC4-0xC003BBD7 __libc_main [/lib/libc.so.6] (20 bytes)
  3820.          ...
  3821.          0xC008F8BC-0xC008FA4D __moddi3 [/lib/libc.so.6] (402 bytes)
  3822.          0xC008FA50-0xC008FB19 __udivdi3 [/lib/libc.so.6] (202 bytes)
  3823.          0xC008FB1C-0xC008FC1B __umoddi3 [/lib/libc.so.6] (256 bytes)
  3824.          0xC008FC1C-0xC008FC4D _fini [/lib/libc.so.6] (50 bytes)
  3825.  
  3826.    The next block of data shows a summary of all freed memory
  3827. allocations.  This is only possible because the `NOFREE' option was
  3828. also given, otherwise there would be no details on freed memory
  3829. allocations.  All of these entries show where the allocation was freed,
  3830. which can be useful if you quickly needed to see where an allocation
  3831. was freed.  The `SHOWFREED' option also displays this information.
  3832.  
  3833.    As this example was run on UNIX, the mpatrol library replaces the
  3834. default implementations of `malloc()', `free()', etc.  As can be seen
  3835. below, this allows the library to trace all calls to allocate dynamic
  3836. memory in a process, even from functions that were not compiled with
  3837. mpatrol.  The two functions shown below were called by the mpatrol
  3838. library in order to read the symbols from ELF object files.  However,
  3839. they are located in the ELF access library which was not compiled with
  3840. mpatrol.
  3841.  
  3842.    Note that the following data has again been cut down in size for the
  3843. purposes of this example.  The `...' marks text that has been removed.
  3844.  
  3845.      freed allocations: 9 (15740 bytes)
  3846.          0x8000A098 (126 bytes) {free:2:0} [-|-|-]
  3847.              0x800011BC elf_end
  3848.              0xC0019668 __mp_init
  3849.              0xC001982A __mp_alloc
  3850.              0x8000099C main
  3851.              0x80000944 _start
  3852.      
  3853.          0x8000A136 (3536 bytes) {free:3:0} [-|-|-]
  3854.              0x8000104E _elf_free
  3855.              0xC0019668 __mp_init
  3856.              0xC001982A __mp_alloc
  3857.              0x8000099C main
  3858.              0x80000944 _start
  3859.      
  3860.          ...
  3861.  
  3862.    The final block of data shows a summary of all unfreed memory
  3863. allocations.  This can show up memory leaks, although the first unfreed
  3864. memory allocation in this example comes from the standard C library.
  3865. On systems such as UNIX it does not really matter about these unfreed
  3866. allocations since they will automatically be returned to the system on
  3867. process termination.
  3868.  
  3869.    However, the second unfreed allocation shows an example of a memory
  3870. leak, where no pointers referencing that allocation remain in the
  3871. program to free it with.  If this was within a loop then the program
  3872. could quickly run away with memory, causing at least a decrease in
  3873. performance, and at most a memory shortage.  The mpatrol library makes
  3874. it easier to spot memory leaks.
  3875.  
  3876.    The `SHOWUNFREED' option also displays this information.
  3877.  
  3878.      unfreed allocations: 2 (108 bytes)
  3879.          0x8000A010 (104 bytes) {malloc:1:0} [-|-|-]
  3880.              0xC0052B4A _IO_fopen
  3881.              0xC0017A0C __mp_openlogfile
  3882.              0xC0019648 __mp_init
  3883.              0xC001982A __mp_alloc
  3884.              0x8000099C main
  3885.              0x80000944 _start
  3886.      
  3887.          0x8000AFC4 (4 bytes) {malloc:10:0} [main|test.c|14]
  3888.              0x8000099C main
  3889.              0x80000944 _start
  3890.  
  3891. 
  3892. File: mpatrol.info,  Node: Tutorial,  Next: Functions,  Prev: Examples,  Up: Top
  3893.  
  3894. Tutorial
  3895. ********
  3896.  
  3897.    In this chapter we'll look at a real example of using the mpatrol
  3898. library to debug a program.  All of the following building and
  3899. debugging steps were performed on a Linux/m68k machine so the details
  3900. may differ slightly on your system, but the concepts should remain the
  3901. same.  However, on systems without virtual memory some of the steps may
  3902. actually cause the machine to lock up or crash so be aware of this if
  3903. you are running such a system -- you may be safer just reading this
  3904. tutorial rather than attempting it!
  3905.  
  3906.    This tutorial will also make use of the option `USEDEBUG' which
  3907. displays source-level file names and line numbers associated with
  3908. symbols in call stack tracebacks, but only if the underlying object
  3909. file access library supports reading line tables from object files and
  3910. even then only if the object files were compiled with debugging
  3911. information enabled.
  3912.  
  3913.    The program we are going to look at is a simple filter which
  3914. processes its standard input and displays the processed information on
  3915. its standard output.  In this case the program converts all lowercase
  3916. characters to uppercase and removes any blank lines.  The source for
  3917. the program is given below, but can also be found in
  3918. `tests/tutorial/test1.c'.
  3919.  
  3920.      23  /*
  3921.      24   * Reads the standard input file stream, converts all lowercase
  3922.      25   * characters to uppercase, and displays all non-empty lines to the
  3923.      26   * standard output file stream.
  3924.      27   */
  3925.      
  3926.      
  3927.      30  #include <stdio.h>
  3928.      31  #include <stdlib.h>
  3929.      32  #include <string.h>
  3930.      33  #include <ctype.h>
  3931.      
  3932.      
  3933.      36  char *strtoupper(char *s)
  3934.      37  {
  3935.      38      char *t;
  3936.      39      size_t i, l;
  3937.      
  3938.      41      l = strlen(s);
  3939.      42      t = (char *) malloc(l);
  3940.      43      for (i = 0; i < l; i++)
  3941.      44          t[i] = toupper(s[i]);
  3942.      45      t[i] = '\0';
  3943.      46      return t;
  3944.      47  }
  3945.      
  3946.      
  3947.      50  int main(void)
  3948.      51  {
  3949.      52      char *b, *s;
  3950.      
  3951.      54      b = (char *) malloc(BUFSIZ);
  3952.      55      while (gets(b))
  3953.      56      {
  3954.      57          s = strtoupper(b);
  3955.      58          if (*s != '\0')
  3956.      59          {
  3957.      60              puts(s);
  3958.      61              free(s);
  3959.      62          }
  3960.      63      }
  3961.      64      free(b);
  3962.      65      return EXIT_SUCCESS;
  3963.      66  }
  3964.  
  3965.    If you quickly skimmed over the above code then you might have
  3966. noticed some rather obvious errors, but there are also some less
  3967. obvious ones hidden there as well.  After compiling and linking with
  3968. the system C compiler and libraries it successfully runs, even when its
  3969. source code is piped to it.  So if it runs, why bother trying to debug
  3970. it?
  3971.  
  3972.    The short answer to that is that this program does in fact contain
  3973. one rather major error that is likely to prevent it from running
  3974. portably on other systems.  However, for the purposes of this tutorial,
  3975. we'll pretend that we've just been handed the source code for this
  3976. program and have not worked on it before.  So let's now try to compile
  3977. and link it with the mpatrol library(1).
  3978.  
  3979.    First, add the inclusion of `mpatrol.h' to line 34 so that we can
  3980. replace calls to `malloc()' and `free()' with their mpatrol
  3981. equivalents(2).  Then, recompile the program and link it with the
  3982. mpatrol library.  This time, running it with even the simplest of
  3983. non-empty input lines should cause it to abort!
  3984.  
  3985.    If you look at the `mpatrol.log' file produced, you should see
  3986. something along the lines of the following at the end of the log file.
  3987.  
  3988.  
  3989.      ERROR: free memory corruption at 0x8000706C
  3990.              0x8000706C  00555555 55555555 55555555 55555555  .UUUUUUUUUUUUUUU
  3991.              0x8000707C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3992.              0x8000708C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3993.              0x8000709C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3994.              0x800070AC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3995.              0x800070BC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3996.              0x800070CC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3997.              0x800070DC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3998.              0x800070EC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  3999.              0x800070FC  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4000.              0x8000710C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4001.              0x8000711C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4002.              0x8000712C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4003.              0x8000713C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4004.              0x8000714C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4005.              0x8000715C  55555555 55555555 55555555 55555555  UUUUUUUUUUUUUUUU
  4006.  
  4007.    This tells us that something has written a zero byte into free
  4008. memory at location `0x8000706C'.  Unfortunately, the library only
  4009. caught it at the next call to one of its functions so it had already
  4010. happened somewhere in between the last call and the current call.
  4011. Turning on the `LOGALL' option in the `MPATROL_OPTIONS' environment
  4012. variable allows us to see the last successful function call to the
  4013. mpatrol library.
  4014.  
  4015.      ALLOC: malloc (50, 8192 bytes, 2 bytes) [main|test1.c|54]
  4016.              0x80000A30 main (/usr/users/homedir/graeme/test1.c:54)
  4017.              0x80000944 _start
  4018.      
  4019.      returns 0x80009000
  4020.      
  4021.      ALLOC: malloc (51, 4 bytes, 2 bytes) [strtoupper|test1.c|42]
  4022.              0x800009AE strtoupper (/usr/users/homedir/graeme/test1.c:42)
  4023.              0x80000A54 main (/usr/users/homedir/graeme/test1.c:57)
  4024.              0x80000944 _start
  4025.      
  4026.      returns 0x80007068
  4027.  
  4028.    Unfortunately, this only tells us that the last successful mpatrol
  4029. library function call was `malloc()' called from `strtoupper()'.  If we
  4030. add the option `OFLOWSIZE=8' to the `MPATROL_OPTIONS' environment
  4031. variable then we get slightly more information about which memory
  4032. allocation was affected(3).
  4033.  
  4034.      ERROR: allocation 0x80007080 has a corrupted overflow buffer at 0x80007084
  4035.              0x80007084  00AAAAAA AAAAAAAA                    ........
  4036.      
  4037.          0x80007080 (4 bytes) {malloc:51:0} [strtoupper|test1.c|42]
  4038.              0x800009AE strtoupper (/usr/users/homedir/graeme/test1.c:42)
  4039.              0x80000A54 main (/usr/users/homedir/graeme/test1.c:57)
  4040.              0x80000944 _start
  4041.  
  4042.    Now we can make a better guess about what is happening.  Since the
  4043. start of the upper overflow buffer of allocation 51 has been written
  4044. to, we can assume that something has written one byte beyond the end of
  4045. that memory allocation.  You can probably see where that is happening
  4046. now by looking at the code, but let's try to be even more sure that
  4047. this is what is wrong.
  4048.  
  4049.    The only foolproof way to do this is to add a software watch point
  4050. to keep an eye on the address that is being written to.  This can
  4051. normally only be done within a debugger, but on systems that support
  4052. programmable software watch points, the `OFLOWWATCH' option can be used
  4053. to do the same thing.  For the sake of generality, we'll use the
  4054. debugger watch point approach, in this case with `gdb'.  In order for
  4055. the following example to work correctly you'll need to add the
  4056. `ALLOCSTOP=51' option to the `MPATROL_OPTIONS' environment variable so
  4057. that we can stop just after the last successful memory allocation.
  4058.  
  4059.      (gdb) break main
  4060.      Breakpoint 1 at 0x80000a10: file test1.c, line 54.
  4061.      (gdb) run
  4062.      Starting program: a.out
  4063.      Breakpoint 1, main() at test1.c:54
  4064.      54          b = malloc(BUFSIZ);
  4065.      (gdb) break __mp_trap
  4066.      Breakpoint 2 at 0xc00182ac
  4067.      (gdb) continue
  4068.      Continuing.
  4069.      test
  4070.      Breakpoint 2, 0xc00182ac in __mp_trap()
  4071.      (gdb) backtrace
  4072.      #0  0xc00182ac in __mp_trap()
  4073.      #1  0xc0016494 in __mp_getmemory()
  4074.      #2  0xc001a618 in __mp_alloc()
  4075.      #3  0x800009ae in strtoupper(s=0x80009008 "test") at test1.c:42
  4076.      #4  0x80000a54 in main() at test1.c:57
  4077.      (gdb) step
  4078.      Single stepping until exit from function __mp_trap,
  4079.      which has no line number information.
  4080.      0xc0016494 in __mp_getmemory()
  4081.      (gdb) step
  4082.      Single stepping until exit from function __mp_getmemory,
  4083.      which has no line number information.
  4084.      0xc001a618 in __mp_alloc()
  4085.      (gdb) step
  4086.      Single stepping until exit from function __mp_alloc,
  4087.      which has no line number information.
  4088.      strtoupper(s=0x80009008 "test") at test1.c:43
  4089.      43          for (i = 0; i < l; i++)
  4090.      (gdb) watch *0x80007084
  4091.      Watchpoint 3: *2147512452
  4092.      (gdb) continue
  4093.      Continuing.
  4094.      Watchpoint 3: *2147512452
  4095.      Old value = -1431655766
  4096.      New value = 11184810
  4097.      strtoupper(s=0x80009008 "test") at test1.c:46
  4098.      46          return t;
  4099.      (gdb) quit
  4100.      The program is running.  Quit anyway (and kill it)? (y or n) y
  4101.  
  4102.    After loading the program into `gdb', we need to break at `main()'
  4103. so that we can run to a point where all of the shared library symbols
  4104. have been loaded into memory(4).  We can then set another breakpoint at
  4105. `__mp_trap()' and continue until allocation 51 has been reached.
  4106.  
  4107.    Because the mpatrol library has not been built with debugging
  4108. information in this example we can quickly step back to the
  4109. `strtoupper()' function since `gdb' won't step through functions that
  4110. have no debugging information.  We then set a watch point on address
  4111. `0x80007084', which is the address of the memory location that has been
  4112. causing the problems.  After continuing, the debugger stops at line 46,
  4113. but this is more likely to be line 45 since that is where a zero byte
  4114. is being written to(5).
  4115.  
  4116.    So, we have located the problem, which is simply a case of not
  4117. allocating enough memory to contain the copied string _and_ the
  4118. terminating zero byte.  We can also improve the `strtoupper()' function
  4119. by checking the pointer returned by `malloc()' to see if it is `NULL',
  4120. and if so simply exit with an error.  You can try running the program
  4121. with the `FAILFREQ' option to see how it would originally behave in a
  4122. low memory situation.
  4123.  
  4124.    The following listing shows the above modifications that we have
  4125. made to our program.  It can also be found in `tests/tutorial/test2.c'.
  4126.  
  4127.      23  /*
  4128.      24   * Reads the standard input file stream, converts all lowercase
  4129.      25   * characters to uppercase, and displays all non-empty lines to the
  4130.      26   * standard output file stream.
  4131.      27   */
  4132.      
  4133.      
  4134.      30  #include <stdio.h>
  4135.      31  #include <stdlib.h>
  4136.      32  #include <string.h>
  4137.      33  #include <ctype.h>
  4138.      34  #include "mpatrol.h"
  4139.      
  4140.      
  4141.      37  char *strtoupper(char *s)
  4142.      38  {
  4143.      39      char *t;
  4144.      40      size_t i, l;
  4145.      
  4146.      42      l = strlen(s);
  4147.      43      if ((t = (char *) malloc(l + 1)) == NULL)
  4148.      44      {
  4149.      45          fputs("strtoupper: out of memory\n", stderr);
  4150.      46          exit(EXIT_FAILURE);
  4151.      47      }
  4152.      48      for (i = 0; i < l; i++)
  4153.      49          t[i] = toupper(s[i]);
  4154.      50      t[i] = '\0';
  4155.      51      return t;
  4156.      52  }
  4157.      
  4158.      
  4159.      55  int main(void)
  4160.      56  {
  4161.      57      char *b, *s;
  4162.      
  4163.      59      b = (char *) malloc(BUFSIZ);
  4164.      60      while (gets(b))
  4165.      61      {
  4166.      62          s = strtoupper(b);
  4167.      63          if (*s != '\0')
  4168.      64          {
  4169.      65              puts(s);
  4170.      66              free(s);
  4171.      67          }
  4172.      68      }
  4173.      69      free(b);
  4174.      70      return EXIT_SUCCESS;
  4175.      71  }
  4176.  
  4177.    Leaving aside the obvious problem with `gets()' and the general
  4178. inefficiency of the algorithm, we could assume that our program works
  4179. safely now and we can release it to the outside world.  However, a user
  4180. soon reports a problem with our program steadily using more and more
  4181. memory during its execution when processing very large files.
  4182.  
  4183.    This is generally attributable to a memory leak and so we can use the
  4184. `SHOWUNFREED' option to try to detect where the memory leak is coming
  4185. from.  Following is some example output from the mpatrol log file when
  4186. our program is run and is given a relatively small text file as input.
  4187.  
  4188.      unfreed allocations: 6 (109 bytes)
  4189.          0x80007000 (104 bytes) {malloc:1:0} [-|-|-]
  4190.              0xC008DB4A _IO_fopen
  4191.              0xC00183DC __mp_openlogfile
  4192.              0xC001A3A4 __mp_init
  4193.              0xC001A584 __mp_alloc
  4194.              0x80000A98 main
  4195.              0x80000980 _start
  4196.      
  4197.          0x80007068 (1 byte) {malloc:52:0} [strtoupper|test2.c|43]
  4198.              0x800009EE strtoupper
  4199.              0x80000ABC main
  4200.              0x80000980 _start
  4201.      
  4202.          0x8000706A (1 byte) {malloc:54:0} [strtoupper|test2.c|43]
  4203.              0x800009EE strtoupper
  4204.              0x80000ABC main
  4205.              0x80000980 _start
  4206.      
  4207.          0x8000706C (1 byte) {malloc:56:0} [strtoupper|test2.c|43]
  4208.              0x800009EE strtoupper
  4209.              0x80000ABC main
  4210.              0x80000980 _start
  4211.      
  4212.          0x8000706E (1 byte) {malloc:58:0} [strtoupper|test2.c|43]
  4213.              0x800009EE strtoupper
  4214.              0x80000ABC main
  4215.              0x80000980 _start
  4216.      
  4217.          0x80007070 (1 byte) {malloc:60:0} [strtoupper|test2.c|43]
  4218.              0x800009EE strtoupper
  4219.              0x80000ABC main
  4220.              0x80000980 _start
  4221.  
  4222.    We can discount the first entry since that is obviously coming from
  4223. when the mpatrol library first initialises itself.  However, all of the
  4224. other entries appear to be coming from line 43 within `strtoupper()'
  4225. and appear to be only 1 byte in length.  At that point in the code, the
  4226. only possible reason for allocating 1 byte is when the string is empty
  4227. and so that must mean that we are not freeing memory that contains
  4228. empty strings.  Looking at line 66 we can see that `free()' is only
  4229. ever called for non-empty strings and therefore if we move the call to
  4230. `free()' outside the test for an empty string we will fix the memory
  4231. leak.  The file `tests/tutorial/test3.c' contains the source for the
  4232. final program.
  4233.  
  4234.    ---------- Footnotes ----------
  4235.  
  4236.    (1) On UNIX systems with dynamic linking it might also be possible
  4237. to run the program under the mpatrol command with its `-d' option
  4238. without having to recompile or relink, but compiling and linking with
  4239. the mpatrol library is a more generic solution across different
  4240. platforms.
  4241.  
  4242.    (2) This is not strictly necessary on UNIX and Windows platforms
  4243. (and AmigaOS when using `gcc'), but it does give us more debugging
  4244. information.
  4245.  
  4246.    (3) Note that the start address of the allocation has changed
  4247. slightly since we added padding around it with the `OFLOWSIZE' option.
  4248.  
  4249.    (4) This is only necessary when the mpatrol library has been built
  4250. as a shared library.
  4251.  
  4252.    (5) This is not necessarily the fault of the debugger or the
  4253. debugging information generated by the compiler since on most platforms
  4254. such watchpoints can only be caught after they occur, hence most
  4255. debuggers show the next statement to be executed rather than the current
  4256. one.
  4257.  
  4258. 
  4259. File: mpatrol.info,  Node: Functions,  Next: Environment,  Prev: Tutorial,  Up: Top
  4260.  
  4261. Functions
  4262. *********
  4263.  
  4264.    The mpatrol library contains implementations of dynamic memory
  4265. allocation functions for C and C++ suitable for tracing and debugging.
  4266. The library is intended to be used without requiring any changes to
  4267. existing user source code except the inclusion of the `mpatrol.h'
  4268. header file, although additional functions are supplied for extra
  4269. tracing and control.  Note that the current version of the mpatrol
  4270. library is contained in the `MPATROL_VERSION' preprocessor macro.
  4271.  
  4272.    All of the function definitions in `mpatrol.h' can be disabled by
  4273. defining the `NDEBUG' preprocessor macro, which is the same macro used
  4274. to control the behaviour of the `assert()' function.  If `NDEBUG' is
  4275. defined then no macro redefinition of functions will take place and all
  4276. special mpatrol library functions will evaluate to empty statements.
  4277. It is intended that the `NDEBUG' preprocessor macro be defined in
  4278. release builds.
  4279.  
  4280.    The following 14 functions are available as replacements for
  4281. existing C library functions.  To use these you must include
  4282. `mpatrol.h' before all other header files, although on UNIX and Windows
  4283. platforms (and AmigaOS when using `gcc') they will be used anyway,
  4284. albeit with slightly less tracing information.
  4285.  
  4286. `void *malloc(size_t size)'
  4287.      Allocates SIZE uninitialised bytes from the heap and returns a
  4288.      pointer to the first byte of the allocation.  The pointer returned
  4289.      will be suitably aligned for casting to any type and can be used
  4290.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4291.      the memory allocated will be implicitly rounded up to `1' byte.
  4292.      If there is not enough space in the heap then the `NULL' pointer
  4293.      will be returned and `errno' will be set to `ENOMEM'.  The
  4294.      allocated memory must be deallocated with `free()' or reallocated
  4295.      with `realloc()'.
  4296.  
  4297. `void *calloc(size_t nelem, size_t size)'
  4298.      Allocates NELEM elements of SIZE zero-initialised bytes from the
  4299.      heap and returns a pointer to the first byte of the allocation.
  4300.      The pointer returned will be suitably aligned for casting to any
  4301.      type and can be used to store data of up to `nelem * size' bytes
  4302.      in length.  If `nelem * size' is `0' then the amount of memory
  4303.      allocated will be implicitly rounded up to `1' byte.  If there is
  4304.      not enough space in the heap then the `NULL' pointer will be
  4305.      returned and `errno' will be set to `ENOMEM'.  The allocated
  4306.      memory must be deallocated with `free()' or reallocated with
  4307.      `realloc()'.
  4308.  
  4309. `void *memalign(size_t align, size_t size)'
  4310.      Allocates SIZE uninitialised bytes from the heap and returns a
  4311.      pointer to the first byte of the allocation.  The pointer returned
  4312.      will be aligned to ALIGN bytes and can be used to store data of up
  4313.      to SIZE bytes in length.  If ALIGN is zero then the default system
  4314.      alignment will be used.  If ALIGN is not a power of two then it
  4315.      will be rounded up to the nearest power of two.  If ALIGN is
  4316.      greater than the system page size then it will be truncated to
  4317.      that value.  If SIZE is `0' then the memory allocated will be
  4318.      implicitly rounded up to `1' byte.  If there is not enough space in
  4319.      the heap then the `NULL' pointer will be returned and `errno' will
  4320.      be set to `ENOMEM'.  The allocated memory must be deallocated with
  4321.      `free()' or reallocated with `realloc()', although the latter will
  4322.      not guarantee the preservation of alignment.
  4323.  
  4324. `void *valloc(size_t size)'
  4325.      Allocates SIZE uninitialised bytes from the heap and returns a
  4326.      pointer to the first byte of the allocation.  The pointer returned
  4327.      will be aligned to the system page size and can be used to store
  4328.      data of up to SIZE bytes in length.  If SIZE is `0' then the
  4329.      memory allocated will be implicitly rounded up to `1' byte.  If
  4330.      there is not enough space in the heap then the `NULL' pointer will
  4331.      be returned and `errno' will be set to `ENOMEM'.  The allocated
  4332.      memory must be deallocated with `free()' or reallocated with
  4333.      `realloc()', although the latter will not guarantee the
  4334.      preservation of alignment.
  4335.  
  4336. `void *pvalloc(size_t size)'
  4337.      Allocates SIZE uninitialised bytes from the heap and returns a
  4338.      pointer to the first byte of the allocation.  The pointer returned
  4339.      will be aligned to the system page size and can be used to store
  4340.      data of up to SIZE bytes in length.  If SIZE is `0' then the
  4341.      memory allocated will be implicitly rounded up to `1' page,
  4342.      otherwise SIZE will be implicitly rounded up to a multiple of the
  4343.      system page size.  If there is not enough space in the heap then
  4344.      the `NULL' pointer will be returned and `errno' will be set to
  4345.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4346.      or reallocated with `realloc()', although the latter will not
  4347.      guarantee the preservation of alignment.
  4348.  
  4349. `char *strdup(const char *str)'
  4350.      Allocates exactly enough memory from the heap to duplicate STR
  4351.      (including the terminating nul character) and returns a pointer to
  4352.      the first byte of the allocation after copying STR to the
  4353.      newly-allocated memory.  The pointer returned will have no
  4354.      alignment constraints and can be used to store character data up
  4355.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4356.      will be returned.  If there is not enough space in the heap then
  4357.      the `NULL' pointer will be returned and `errno' will be set to
  4358.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4359.      or reallocated with `realloc()'.
  4360.  
  4361. `char *strndup(const char *str, size_t size)'
  4362.      Allocates exactly enough memory from the heap to duplicate STR
  4363.      (including the terminating nul character) and returns a pointer to
  4364.      the first byte of the allocation after copying STR to the
  4365.      newly-allocated memory.  The pointer returned will have no
  4366.      alignment constraints and can be used to store character data up
  4367.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4368.      will be returned.  If the length of STR is greater than SIZE then
  4369.      only SIZE characters will be allocated and copied, with one
  4370.      additional byte for the nul character.  If there is not enough
  4371.      space in the heap then the `NULL' pointer will be returned and
  4372.      `errno' will be set to `ENOMEM'.  The allocated memory must be
  4373.      deallocated with `free()' or reallocated with `realloc()'.  This
  4374.      function is available for backwards compatibility with older C
  4375.      libraries and should not be used in new code.
  4376.  
  4377. `char *strsave(const char *str)'
  4378.      Allocates exactly enough memory from the heap to duplicate STR
  4379.      (including the terminating nul character) and returns a pointer to
  4380.      the first byte of the allocation after copying STR to the
  4381.      newly-allocated memory.  The pointer returned will have no
  4382.      alignment constraints and can be used to store character data up
  4383.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4384.      will be returned.  If there is not enough space in the heap then
  4385.      the `NULL' pointer will be returned and `errno' will be set to
  4386.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4387.      or reallocated with `realloc()'.  This function is available for
  4388.      backwards compatibility with older C libraries and should not be
  4389.      used in new code.
  4390.  
  4391. `char *strnsave(const char *str, size_t size)'
  4392.      Allocates exactly enough memory from the heap to duplicate STR
  4393.      (including the terminating nul character) and returns a pointer to
  4394.      the first byte of the allocation after copying STR to the
  4395.      newly-allocated memory.  The pointer returned will have no
  4396.      alignment constraints and can be used to store character data up
  4397.      to the length of STR.  If STR is `NULL' then the `NULL' pointer
  4398.      will be returned.  If the length of STR is greater than SIZE then
  4399.      only SIZE characters will be allocated and copied, with one
  4400.      additional byte for the nul character.  If there is not enough
  4401.      space in the heap then the `NULL' pointer will be returned and
  4402.      `errno' will be set to `ENOMEM'.  The allocated memory must be
  4403.      deallocated with `free()' or reallocated with `realloc()'.  This
  4404.      function is available for backwards compatibility with older C
  4405.      libraries and should not be used in new code.
  4406.  
  4407. `void *realloc(void *ptr, size_t size)'
  4408.      Resizes the memory allocation beginning at PTR to SIZE bytes and
  4409.      returns a pointer to the first byte of the new allocation after
  4410.      copying PTR to the newly-allocated memory, which will be truncated
  4411.      if SIZE is smaller than the original allocation.  The pointer
  4412.      returned will be suitably aligned for casting to any type and can
  4413.      be used to store data of up to SIZE bytes in length.  If PTR is
  4414.      `NULL' then the call will be equivalent to `malloc()'.  If SIZE is
  4415.      `0' then the existing memory allocation will be freed and the
  4416.      `NULL' pointer will be returned.  If SIZE is greater than the
  4417.      original allocation then the extra space will be filled with
  4418.      uninitialised bytes.  If there is not enough space in the heap
  4419.      then the `NULL' pointer will be returned and `errno' will be set to
  4420.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4421.      and can be reallocated again with `realloc()'.
  4422.  
  4423. `void *recalloc(void *ptr, size_t nelem, size_t size)'
  4424.      Resizes the memory allocation beginning at PTR to NELEM elements of
  4425.      SIZE bytes and returns a pointer to the first byte of the new
  4426.      allocation after copying PTR to the newly-allocated memory, which
  4427.      will be truncated if `nelem * size' is smaller than the original
  4428.      allocation.  The pointer returned will be suitably aligned for
  4429.      casting to any type and can be used to store data of up to `nelem
  4430.      * size' bytes in length.  If PTR is `NULL' then the call will be
  4431.      equivalent to `calloc()'.  If `nelem * size' is `0' then the
  4432.      existing memory allocation will be freed and the `NULL' pointer
  4433.      will be returned.  If `nelem * size' is greater than the original
  4434.      allocation then the extra space will be filled with
  4435.      zero-initialised bytes.  If there is not enough space in the heap
  4436.      then the `NULL' pointer will be returned and `errno' will be set to
  4437.      `ENOMEM'.  The allocated memory must be deallocated with `free()'
  4438.      and can be reallocated again with `realloc()'.  This function is
  4439.      available for backwards compatibility with older C libraries and
  4440.      `calloc()' and should not be used in new code.
  4441.  
  4442. `void *expand(void *ptr, size_t size)'
  4443.      Attempts to resize the memory allocation beginning at PTR to SIZE
  4444.      bytes and either returns PTR if there was enough space to resize
  4445.      it, or `NULL' if the block could not be resized for a particular
  4446.      reason.  If PTR is `NULL' then the call will be equivalent to
  4447.      `malloc()'.  If SIZE is `0' then the existing memory allocation
  4448.      will be freed and the `NULL' pointer will be returned.  If SIZE is
  4449.      greater than the original allocation then the extra space will be
  4450.      filled with uninitialised bytes and if SIZE is less than the
  4451.      original allocation then the memory block will be truncated.  If
  4452.      there is not enough space in the heap then the `NULL' pointer will
  4453.      be returned and `errno' will be set to `ENOMEM'.  The allocated
  4454.      memory must be deallocated with `free()' and can be reallocated
  4455.      again with `realloc()'.  This function is available for backwards
  4456.      compatibility with older C libraries and should not be used in new
  4457.      code.
  4458.  
  4459. `void free(void *ptr)'
  4460.      Frees the memory allocation beginning at PTR so the memory can be
  4461.      reused by another call to allocate memory.  If PTR is `NULL' then
  4462.      no memory will be freed.  All of the previous contents will be
  4463.      destroyed.
  4464.  
  4465. `void cfree(void *ptr, size_t nelem, size_t size)'
  4466.      Frees the memory allocation beginning at PTR so the memory can be
  4467.      reused by another call to allocate memory.  If PTR is `NULL' then
  4468.      no memory will be freed.  All of the previous contents will be
  4469.      destroyed.  The NELEM and SIZE parameters are ignored in this
  4470.      implementation.  This function is available for backwards
  4471.      compatibility with older C libraries and `calloc()' and should not
  4472.      be used in new code.
  4473.  
  4474.    The following 5 functions are available as replacements for existing
  4475. C++ library functions, but the replacements in `mpatrol.h' will only be
  4476. used if the `MP_NOCPLUSPLUS' preprocessor macro is not defined.  To use
  4477. these you must include `mpatrol.h' before all other header files,
  4478. although on UNIX and Windows platforms (and AmigaOS when using `gcc')
  4479. they will be used anyway, albeit with slightly less tracing information.
  4480.  
  4481. `void *operator new(size_t size)'
  4482.      Allocates SIZE uninitialised bytes from the heap and returns a
  4483.      pointer to the first byte of the allocation.  The pointer returned
  4484.      will be suitably aligned for casting to any type and can be used
  4485.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4486.      the memory allocated will be implicitly rounded up to `1' byte.
  4487.      If there is not enough space in the heap then the `NULL' pointer
  4488.      will be returned and `errno' will be set to `ENOMEM' -- no
  4489.      exceptions will be thrown.  The allocated memory must be
  4490.      deallocated with `operator delete'.
  4491.  
  4492. `void *operator new[](size_t size)'
  4493.      Allocates SIZE uninitialised bytes from the heap and returns a
  4494.      pointer to the first byte of the allocation.  The pointer returned
  4495.      will be suitably aligned for casting to any type and can be used
  4496.      to store data of up to SIZE bytes in length.  If SIZE is `0' then
  4497.      the memory allocated will be implicitly rounded up to `1' byte.
  4498.      If there is not enough space in the heap then the `NULL' pointer
  4499.      will be returned and `errno' will be set to `ENOMEM' -- no
  4500.      exceptions will be thrown.  The allocated memory must be
  4501.      deallocated with `operator delete[]'.
  4502.  
  4503. `void operator delete(void *ptr)'
  4504.      Frees the memory allocation beginning at PTR so the memory can be
  4505.      reused by another call to allocate memory.  If PTR is `NULL' then
  4506.      no memory will be freed.  All of the previous contents will be
  4507.      destroyed.  This function must only be used with memory allocated
  4508.      by `operator new'.
  4509.  
  4510. `void operator delete[](void *ptr)'
  4511.      Frees the memory allocation beginning at PTR so the memory can be
  4512.      reused by another call to allocate memory.  If PTR is `NULL' then
  4513.      no memory will be freed.  All of the previous contents will be
  4514.      destroyed.  This function must only be used with memory allocated
  4515.      by `operator new[]'.
  4516.  
  4517. `void (*set_new_handler(void (*func)(void)))(void)'
  4518.      Installs a low-memory handler specifically for use with `operator
  4519.      new' and `operator new[]' and returns a pointer to the previously
  4520.      installed handler, or the `NULL' pointer if no handler had been
  4521.      previously installed.  This will be called repeatedly by both
  4522.      functions when they would normally return `NULL', and this loop
  4523.      will continue until they manage to allocate the requested space.
  4524.      The default low-memory handler for the C++ operators will
  4525.      terminate the program and write an out of memory message to the
  4526.      log file.  Note that this function is equivalent to
  4527.      `__mp_nomemory()' and will replace the handler installed by that
  4528.      function.
  4529.  
  4530.    The following 10 functions are available as replacements for
  4531. existing C library memory operation functions.  To use these you must
  4532. include `mpatrol.h' before all other header files, although on UNIX and
  4533. Windows platforms (and AmigaOS when using `gcc') they will be used
  4534. anyway, albeit with slightly less tracing information.
  4535.  
  4536. `void *memset(void *ptr, int byte, size_t size)'
  4537.      Writes SIZE bytes of value BYTE to the memory location beginning at
  4538.      PTR and returns PTR.  If SIZE is `0' then no bytes will be
  4539.      written.  If the operation would affect an existing memory
  4540.      allocation in the heap but would straddle that allocation's
  4541.      boundaries then an error message will be generated in the log file
  4542.      and no bytes will be written.
  4543.  
  4544. `void bzero(void *ptr, size_t size)'
  4545.      Writes SIZE zero bytes to the memory location beginning at PTR.  If
  4546.      SIZE is `0' then no bytes will be written.  If the operation would
  4547.      affect an existing memory allocation in the heap but would
  4548.      straddle that allocation's boundaries then an error message will
  4549.      be generated in the log file and no bytes will be written.  This
  4550.      function is available for backwards compatibility with older C
  4551.      libraries and should not be used in new code.
  4552.  
  4553. `void *memccpy(void *dest, const void *src, int byte, size_t size)'
  4554.      Copies SIZE bytes from SRC to DEST and returns `NULL', or copies
  4555.      the number of bytes up to and including the first occurrence of
  4556.      BYTE if BYTE exists within the specified range and returns a
  4557.      pointer to the first byte after BYTE.  If SIZE is `0' or SRC is
  4558.      the same as DEST then no bytes will be copied.  The source and
  4559.      destination ranges should not overlap, otherwise a warning will be
  4560.      written to the log file.  If the operation would affect an
  4561.      existing memory allocation in the heap but would straddle that
  4562.      allocation's boundaries then an error message will be generated in
  4563.      the log file and no bytes will be copied.
  4564.  
  4565. `void *memcpy(void *dest, const void *src, size_t size)'
  4566.      Copies SIZE bytes from SRC to DEST and returns DEST.  If SIZE is
  4567.      `0' or SRC is the same as DEST then no bytes will be copied.  The
  4568.      source and destination ranges should not overlap, otherwise a
  4569.      warning will be written to the log file.  If the operation would
  4570.      affect an existing memory allocation in the heap but would
  4571.      straddle that allocation's boundaries then an error message will
  4572.      be generated in the log file and no bytes will be copied.
  4573.  
  4574. `void *memmove(void *dest, const void *src, size_t size)'
  4575.      Copies SIZE bytes from SRC to DEST and returns DEST.  If SIZE is
  4576.      `0' or SRC is the same as DEST then no bytes will be copied.  If
  4577.      the operation would affect an existing memory allocation in the
  4578.      heap but would straddle that allocation's boundaries then an error
  4579.      message will be generated in the log file and no bytes will be
  4580.      copied.
  4581.  
  4582. `void bcopy(const void *src, void *dest, size_t size)'
  4583.      Copies SIZE bytes from SRC to DEST.  If SIZE is `0' or SRC is the
  4584.      same as DEST then no bytes will be copied.  If the operation would
  4585.      affect an existing memory allocation in the heap but would
  4586.      straddle that allocation's boundaries then an error message will
  4587.      be generated in the log file and no bytes will be copied.  This
  4588.      function is available for backwards compatibility with older C
  4589.      libraries and should not be used in new code.
  4590.  
  4591. `int memcmp(const void *ptr1, const void *ptr2, size_t size)'
  4592.      Compares SIZE bytes from PTR1 and PTR2 and returns `0' if all of
  4593.      the bytes are identical, or returns the byte difference of the
  4594.      first differing bytes.  If SIZE is `0' or PTR1 is the same as PTR2
  4595.      then no bytes will be compared.  If the operation would read from
  4596.      an existing memory allocation in the heap but would straddle that
  4597.      allocation's boundaries then an error message will be generated in
  4598.      the log file and no bytes will be compared.
  4599.  
  4600. `int bcmp(const void *ptr1, const void *ptr2, size_t size)'
  4601.      Compares SIZE bytes from PTR1 and PTR2 and returns `0' if all of
  4602.      the bytes are identical, or returns the byte difference of the
  4603.      first differing bytes.  If SIZE is `0' or PTR1 is the same as PTR2
  4604.      then no bytes will be compared.  If the operation would read from
  4605.      an existing memory allocation in the heap but would straddle that
  4606.      allocation's boundaries then an error message will be generated in
  4607.      the log file and no bytes will be compared.  This function is
  4608.      available for backwards compatibility with older C libraries and
  4609.      should not be used in new code.
  4610.  
  4611. `void *memchr(const void *ptr, int byte, size_t size)'
  4612.      Searches up to SIZE bytes in PTR for the first occurrence of BYTE
  4613.      and returns a pointer to it or `NULL' if no such byte occurs.  If
  4614.      SIZE is `0' then no bytes will be searched.  If the operation
  4615.      would affect an existing memory allocation in the heap but would
  4616.      straddle that allocation's boundaries then an error message will
  4617.      be generated in the log file and no bytes will be searched.
  4618.  
  4619. `void *memmem(const void *ptr1, size_t size1, const void *ptr2, size_t size2)'
  4620.      Searches up to SIZE1 bytes in PTR1 for the first occurrence of
  4621.      PTR2 (which is exactly SIZE2 bytes in length) and returns a pointer
  4622.      to it or `NULL' if no such sequence of bytes occur.  If SIZE1 or
  4623.      SIZE2 is `0' then no bytes will be searched.  If the operation
  4624.      would affect an existing memory allocation in the heap but would
  4625.      straddle that allocation's boundaries then an error message will
  4626.      be generated in the log file and no bytes will be searched.
  4627.  
  4628.    The following 8 functions are available as support routines for
  4629. additional control and tracing in the mpatrol library.  To use these
  4630. you should include the `mpatrol.h' header file.
  4631.  
  4632. `int __mp_info(const void *ptr, __mp_allocinfo *info)'
  4633.      Obtains information about a specific memory allocation by placing
  4634.      statistics about PTR in INFO.  If PTR does not belong to a
  4635.      previously allocated memory allocation then `0' will be returned,
  4636.      otherwise `1' will be returned and INFO will contain the following
  4637.      information:
  4638.  
  4639.      _Field_       _Description_
  4640.      `block'       Pointer to first byte of allocation.
  4641.      `size'        Size of allocation in bytes.
  4642.      `type'        Type of function which allocated memory.
  4643.      `alloc'       Allocation index.
  4644.      `realloc'     Number of times reallocated.
  4645.      `thread'      Thread identifier.
  4646.      `func'        Function in which allocation took place.
  4647.      `file'        File in which allocation took place.
  4648.      `line'        Line number at which allocation took place.
  4649.      `stack'       Pointer to function call stack.
  4650.      `freed'       Indicates if allocation has been freed.
  4651.  
  4652. `int __mp_printinfo(const void *ptr)'
  4653.      Displays information about a specific memory allocation containing
  4654.      PTR to the standard error file stream.  If PTR does not belong to
  4655.      a previously allocated memory allocation then `0' will be
  4656.      returned, otherwise `1' will be returned.  This function is
  4657.      intended to be called from within a debugger.
  4658.  
  4659. `void __mp_memorymap(int stats)'
  4660.      If STATS is non-zero then the current statistics of the mpatrol
  4661.      library will be displayed.  If the heap contains at least one
  4662.      allocated, freed or free block then a map of the current heap will
  4663.      also be displayed.
  4664.  
  4665. `void __mp_summary(void)'
  4666.      Displays information about the current state of the mpatrol
  4667.      library, including its settings and any relevant statistics.
  4668.  
  4669. `void __mp_check(void)'
  4670.      Forces the library to perform an immediate check of the overflow
  4671.      buffers of every memory allocation and to ensure that nothing has
  4672.      overwritten any free blocks.
  4673.  
  4674. `void (*__mp_prologue(void (*func)(const void *, size_t)))(const void *, size_t)'
  4675.      Installs a prologue function to be called before any memory
  4676.      allocation, reallocation or deallocation function.  This function
  4677.      will return a pointer to the previously installed prologue
  4678.      function, or the `NULL' pointer if no prologue function had been
  4679.      previously installed.  The following arguments will be used to
  4680.      call the prologue function:
  4681.  
  4682.      _Argument 1_  _Argument 2_  _Called by_
  4683.      `-1'          SIZE          `malloc()', etc.
  4684.      PTR           SIZE          `realloc()', etc.
  4685.      PTR           `-1'          `free()', etc.
  4686.      PTR           `-2'          `strdup()', etc.
  4687.  
  4688. `void (*__mp_epilogue(void (*func)(const void *)))(const void *)'
  4689.      Installs an epilogue function to be called after any memory
  4690.      allocation, reallocation or deallocation function.  This function
  4691.      will return a pointer to the previously installed epilogue
  4692.      function, or the `NULL' pointer if no epilogue function had been
  4693.      previously installed.  The following arguments will be used to
  4694.      call the epilogue function:
  4695.  
  4696.      _Argument_    _Called by_
  4697.      PTR           `malloc()', `realloc()', `strdup()', etc.
  4698.      `-1'          `free()', etc.
  4699.  
  4700. `void (*__mp_nomemory(void (*func)(void)))(void)'
  4701.      Installs a low-memory handler and returns a pointer to the
  4702.      previously installed handler, or the `NULL' pointer if no handler
  4703.      had been previously installed.  This will be called once by C
  4704.      memory allocation functions, and repeatedly by C++ memory
  4705.      allocation functions, when they would normally return `NULL'.  Note
  4706.      that this function is equivalent to `set_new_handler()' and will
  4707.      replace the handler installed by that function.
  4708.  
  4709. 
  4710. File: mpatrol.info,  Node: Environment,  Next: Options,  Prev: Functions,  Up: Top
  4711.  
  4712. Environment
  4713. ***********
  4714.  
  4715.    The library can read certain options at run-time from an environment
  4716. variable called `MPATROL_OPTIONS'.  This variable must contain one or
  4717. more valid option keywords from the list below and must be no longer
  4718. than 1024 characters in length.  If `MPATROL_OPTIONS' is unset or empty
  4719. then the default settings will be used.
  4720.  
  4721.    The syntax for options specified within the `MPATROL_OPTIONS'
  4722. environment variable is `OPTION' or `OPTION=VALUE', where `OPTION' is a
  4723. keyword from the list below and `VALUE' is the setting for that option.
  4724. If `VALUE' is numeric then it may be specified using binary, octal,
  4725. decimal or hexadecimal notation, with binary notation beginning with
  4726. either `0b' or `0B'.  If `VALUE' is a character string containing
  4727. spaces then it may be quoted using double quotes.  No whitespace may
  4728. appear between the `=' sign, but whitespace must appear between
  4729. different options.  Note that option keywords can be given in lowercase
  4730. as well as uppercase, or a mixture of both.
  4731.  
  4732. `ALLOCBYTE'=<UNSIGNED-INTEGER>
  4733.      Specifies an 8-bit byte pattern with which to prefill
  4734.      newly-allocated memory.  This can be used to detect the use of
  4735.      memory which has not been initialised after allocation.  Note that
  4736.      this setting will not affect memory allocated with `calloc()' or
  4737.      `recalloc()' as these functions always prefill allocated memory
  4738.      with an 8-bit byte pattern of zero.  Default value:
  4739.      `ALLOCBYTE=0xFF'.
  4740.  
  4741. `ALLOCSTOP'=<UNSIGNED-INTEGER>
  4742.      Specifies an allocation index at which to stop the program when it
  4743.      is being allocated.  When the number of memory allocations reaches
  4744.      this number the program will be halted, and its state may be
  4745.      examined at that point by using a suitable debugger.  Note that
  4746.      this setting will be ignored if its value is zero.  Default value:
  4747.      `ALLOCSTOP=0'.
  4748.  
  4749. `ALLOWOFLOW'
  4750.      Specifies that a warning rather than an error should be produced
  4751.      if any memory operation function overflows the boundaries of a
  4752.      memory allocation, and that the operation should still be
  4753.      performed.  This option is provided for circumstances where it is
  4754.      desirable for the memory operation to be performed, regardless of
  4755.      whether it is erroneous or not.
  4756.  
  4757. `AUTOSAVE'=<UNSIGNED-INTEGER>
  4758.      Specifies the frequency at which to periodically write the
  4759.      profiling data to the profiling output file.  When the total
  4760.      number of profiled memory allocations and deallocations is a
  4761.      multiple of this number then the current profiling information
  4762.      will be written to the profiling output file.  This option can be
  4763.      used to instruct the mpatrol library to dump out any profiling
  4764.      information just before a fatal error occurs in a program, for
  4765.      example.  Note that this setting will be ignored if its value is
  4766.      zero.  Default value: `AUTOSAVE=0'.
  4767.  
  4768. `CHECK'=<UNSIGNED-RANGE>
  4769.      Specifies a range of allocation indices at which to check the
  4770.      integrity of free memory and overflow buffers.  The range must be
  4771.      specified as no more than two unsigned integers separated by a
  4772.      dash.  If numbers on either the left side or the right side of the
  4773.      dash are omitted then they will be assumed to be `0' and INFINITY
  4774.      respectively.  A value of `0' on its own indicates that no such
  4775.      checking will ever be performed.  This option can be used to speed
  4776.      up the execution speed of the library at the expense of checking.
  4777.      Default value: `CHECK=-'.
  4778.  
  4779. `CHECKALL'
  4780.      Equivalent to the `CHECKALLOCS', `CHECKREALLOCS' and `CHECKFREES'
  4781.      options specified together.
  4782.  
  4783. `CHECKALLOCS'
  4784.      Checks that no attempt is made to allocate a block of memory of
  4785.      size zero.  A warning will be issued for every such case.
  4786.  
  4787. `CHECKFREES'
  4788.      Checks that no attempt is made to deallocate a `NULL' pointer.  A
  4789.      warning will be issued for every such case.
  4790.  
  4791. `CHECKREALLOCS'
  4792.      Checks that no attempt is made to reallocate a `NULL' pointer or
  4793.      resize an existing block of memory to size zero.  Warnings will be
  4794.      issued for every such case.
  4795.  
  4796. `DEFALIGN'=<UNSIGNED-INTEGER>
  4797.      Specifies the default alignment for general-purpose memory
  4798.      allocations, which must be a power of two (and will be rounded up
  4799.      to the nearest power of two if it is not).  The default alignment
  4800.      for a particular system is calculated at run-time.
  4801.  
  4802. `FAILFREQ'=<UNSIGNED-INTEGER>
  4803.      Specifies the frequency at which all memory allocations will
  4804.      randomly fail.  For example, a value of `10' will mean that
  4805.      roughly 1 in 10 memory allocations will fail, but a value of `0'
  4806.      will disable all random failures.  This option can be useful for
  4807.      stress-testing an application.  Default value: `FAILFREQ=0'.
  4808.  
  4809. `FAILSEED'=<UNSIGNED-INTEGER>
  4810.      Specifies the random number seed which will be used when
  4811.      determining which memory allocations will randomly fail.  A value
  4812.      of `0' will instruct the library to pick a random seed every time
  4813.      it is run.  Any other value will mean that the random failures
  4814.      will be the same every time the program is run, but only as long
  4815.      as the seed stays the same.  Default value: `FAILSEED=0'.
  4816.  
  4817. `FREEBYTE'=<UNSIGNED-INTEGER>
  4818.      Specifies an 8-bit byte pattern with which to prefill newly-freed
  4819.      memory.  This can be used to detect the use of memory which has
  4820.      just been freed.  It is also used internally to ensure that freed
  4821.      memory has not been overwritten.  Note that the freed memory may
  4822.      be reused the next time a block of memory is allocated and so once
  4823.      memory has been freed its contents are not guaranteed to remain
  4824.      the same as the specified byte pattern.  Default value:
  4825.      `FREEBYTE=0x55'.
  4826.  
  4827. `FREESTOP'=<UNSIGNED-INTEGER>
  4828.      Specifies an allocation index at which to stop the program when it
  4829.      is being freed.  When the memory allocation with the specified
  4830.      allocation index is to be freed the program will be halted, and
  4831.      its state may be examined at that point using a suitable debugger.
  4832.      Note that this setting will be ignored if its value is zero.
  4833.      Default value: `FREESTOP=0'.
  4834.  
  4835. `HELP'
  4836.      Displays a quick-reference option summary to the `stderr' file
  4837.      stream.
  4838.  
  4839. `LARGEBOUND'=<UNSIGNED-INTEGER>
  4840.      Specifies the limit in bytes up to which memory allocations should
  4841.      be classified as large allocations for profiling purposes.  This
  4842.      limit must be greater than the small and medium bounds.  Default
  4843.      value: `LARGEBOUND=2048'.
  4844.  
  4845. `LIMIT'=<UNSIGNED-INTEGER>
  4846.      Specifies the limit in bytes at which all memory allocations
  4847.      should fail if the total allocated memory should increase beyond
  4848.      this.  This can be used to stress-test software to see how it
  4849.      behaves in low memory conditions.  The internal memory used by the
  4850.      library itself will not be counted as part of the total heap size,
  4851.      but on some systems there may be a small amount of memory required
  4852.      to initialise the library itself.  Note that this setting will be
  4853.      ignored if its value is zero.  Default value: `LIMIT=0'.
  4854.  
  4855. `LOGALL'
  4856.      Equivalent to the `LOGALLOCS', `LOGREALLOCS', `LOGFREES' and
  4857.      `LOGMEMORY' options specified together.
  4858.  
  4859. `LOGALLOCS'
  4860.      Specifies that all memory allocations are to be logged and sent to
  4861.      the log file.  Note that any memory allocations made internally by
  4862.      the library will not be logged.
  4863.  
  4864. `LOGFILE'=<STRING>
  4865.      Specifies an alternative file in which to place all diagnostics
  4866.      from the mpatrol library.  A filename of `stderr' will send all
  4867.      diagnostics to the `stderr' file stream and a filename of `stdout'
  4868.      will do the equivalent with the `stdout' file stream.  Note that
  4869.      if a problem occurs while opening the log file or if any
  4870.      diagnostics require to be displayed before the log file has had a
  4871.      chance to be opened then they will be sent to the `stderr' file
  4872.      stream.  Default value: `LOGFILE=mpatrol.log'.
  4873.  
  4874. `LOGFREES'
  4875.      Specifies that all memory deallocations are to be logged and sent
  4876.      to the log file.  Note that any memory deallocations made
  4877.      internally by the library will not be logged.
  4878.  
  4879. `LOGMEMORY'
  4880.      Specifies that all memory operations are to be logged and sent to
  4881.      the log file.  These operations will be made by calls to functions
  4882.      such as `memset()' and `memcpy()'.  Note that any memory
  4883.      operations made internally by the library will not be logged.
  4884.  
  4885. `LOGREALLOCS'
  4886.      Specifies that all memory reallocations are to be logged and sent
  4887.      to the log file.  Note that any memory reallocations made
  4888.      internally by the library will not be logged.
  4889.  
  4890. `MEDIUMBOUND'=<UNSIGNED-INTEGER>
  4891.      Specifies the limit in bytes up to which memory allocations should
  4892.      be classified as medium allocations for profiling purposes.  This
  4893.      limit must be greater than the small bound but less than the large
  4894.      bound.  Default value: `MEDIUMBOUND=256'.
  4895.  
  4896. `NOFREE'
  4897.      Specifies that the mpatrol library should keep all reallocated and
  4898.      freed memory allocations.  Such freed memory allocations will then
  4899.      be flagged as freed and can be used by the library to provide
  4900.      better diagnostics.  However, as no system memory will ever be
  4901.      reused by the mpatrol library, this option can quickly lead to a
  4902.      shortage of available system memory for a process.  Note that this
  4903.      option will always force a memory reallocation to return a pointer
  4904.      to newly-allocated memory, but the `expand()' function will never
  4905.      be affected by this option.
  4906.  
  4907. `NOPROTECT'
  4908.      Specifies that the mpatrol library's internal data structures
  4909.      should not be made read-only after every memory allocation
  4910.      reallocation or deallocation.  This may significantly speed up
  4911.      execution but this will be at the expense of less safety if the
  4912.      program accidentally overwrites some of the library's internal data
  4913.      structures.  Note that this option has no effect on systems that
  4914.      do not support memory protection.
  4915.  
  4916. `OFLOWBYTE'=<UNSIGNED-INTEGER>
  4917.      Specifies an 8-bit byte pattern with which to fill the overflow
  4918.      buffers of all memory allocations.  This is used internally to
  4919.      ensure that nothing has been written beyond the beginning or the
  4920.      end of a block of allocated memory.  Note that this setting will
  4921.      only have an effect if the `OFLOWSIZE' option is in use.  Default
  4922.      value: `OFLOWBYTE=0xAA'.
  4923.  
  4924. `OFLOWSIZE'=<UNSIGNED-INTEGER>
  4925.      Specifies the size in bytes to use for all overflow buffers, which
  4926.      must be a power of two (and will be rounded up to the nearest
  4927.      power of two if it is not).  This is used internally to ensure
  4928.      that nothing has been written beyond the beginning or the end of a
  4929.      block of allocated memory.  Note that this setting specifies the
  4930.      size for only one of the overflow buffers given to each memory
  4931.      allocation; the other overflow buffer will have an identical size.
  4932.      No overflow buffers will be used if this setting is zero.
  4933.      Default value: `OFLOWSIZE=0'.
  4934.  
  4935. `OFLOWWATCH'
  4936.      Specifies that watch point areas should be used for overflow
  4937.      buffers rather than filling with the overflow byte.  This can
  4938.      significantly reduce the speed of program execution.  Note that
  4939.      this option has no effect on systems that do not support watch
  4940.      point areas.
  4941.  
  4942. `PAGEALLOC'=<`LOWER'|`UPPER'>
  4943.      Specifies that each individual memory allocation should occupy at
  4944.      least one page of virtual memory and should be placed at the
  4945.      lowest or highest point within these pages.  This allows the
  4946.      library to place an overflow buffer of one page on either side of
  4947.      every memory allocation and write-protect these pages as well as
  4948.      all free and freed memory.  Note that this option has no effect on
  4949.      systems that do not support memory protection, and is disabled by
  4950.      default on other systems as it can slow down the speed of program
  4951.      execution.
  4952.  
  4953. `PRESERVE'
  4954.      Specifies that any reallocated or freed memory allocations should
  4955.      preserve their original contents.  This option must be used with
  4956.      the `NOFREE' option and has no effect otherwise.
  4957.  
  4958. `PROF'
  4959.      Specifies that all memory allocations and deallocations are to be
  4960.      profiled and sent to the profiling output file.  Memory
  4961.      reallocations are treated as a memory deallocation immediately
  4962.      followed by a memory allocation.
  4963.  
  4964. `PROFFILE'=<STRING>
  4965.      Specifies an alternative file in which to place all memory
  4966.      allocation profiling information from the mpatrol library.  A
  4967.      filename of `stderr' will send this information to the `stderr'
  4968.      file stream and a filename of `stdout' will do the equivalent with
  4969.      the `stdout' file stream.  Note that if a problem occurs while
  4970.      opening the profiling output file then the profiling information
  4971.      will be sent to the `stderr' file stream.  Default value:
  4972.      `PROFFILE=mpatrol.out'.
  4973.  
  4974. `PROGFILE'=<STRING>
  4975.      Specifies an alternative filename with which to locate the
  4976.      executable file containing the program's symbols.  On most
  4977.      systems, the library will automatically be able to determine this
  4978.      filename, but on a few systems this option may have to be used
  4979.      before any or all symbols can be read.
  4980.  
  4981. `REALLOCSTOP'=<UNSIGNED-INTEGER>
  4982.      Specifies a reallocation index at which to stop the program when a
  4983.      memory allocation is being reallocated.  If the `ALLOCSTOP' option
  4984.      is non-zero then the program will be halted when the allocation
  4985.      matching that allocation index is reallocated the specified number
  4986.      of times.  Otherwise the program will be halted the first time any
  4987.      allocation is reallocated the specified number of times.  Note
  4988.      that this setting will be ignored if its value is zero.  Default
  4989.      value: `REALLOCSTOP=0'.
  4990.  
  4991. `SAFESIGNALS'
  4992.      Instructs the library to save and replace certain signal handlers
  4993.      during the execution of library code and to restore them
  4994.      afterwards.  This was the default behaviour in version 1.0 of the
  4995.      mpatrol library and was changed since some memory-intensive
  4996.      programs became very hard to interrupt using the keyboard, thus
  4997.      giving the impression that the program or system had hung.
  4998.  
  4999. `SHOWALL'
  5000.      Equivalent to the `SHOWFREED', `SHOWUNFREED', `SHOWMAP' and
  5001.      `SHOWSYMBOLS' options specified together.
  5002.  
  5003. `SHOWFREED'
  5004.      Specifies that a summary of all of the freed memory allocations
  5005.      should be displayed at the end of program execution.  This option
  5006.      must be used in conjunction with the `NOFREE' option and this step
  5007.      will not be performed if an abnormal termination occurs or if
  5008.      there were no freed allocations.
  5009.  
  5010. `SHOWMAP'
  5011.      Specifies that a memory map of the entire heap should be displayed
  5012.      at the end of program execution.  This step will not be performed
  5013.      if an abnormal termination occurs or if the heap is empty.
  5014.  
  5015. `SHOWSYMBOLS'
  5016.      Specifies that a summary of all of the function symbols read from
  5017.      the program's executable file should be displayed at the end of
  5018.      program execution.  This step will not be performed if an abnormal
  5019.      termination occurs or if no symbols could be read from the
  5020.      executable file.
  5021.  
  5022. `SHOWUNFREED'
  5023.      Specifies that a summary of all of the unfreed memory allocations
  5024.      should be displayed at the end of program execution.  This step
  5025.      will not be performed if an abnormal termination occurs or if
  5026.      there are no unfreed allocations.
  5027.  
  5028. `SMALLBOUND'=<UNSIGNED-INTEGER>
  5029.      Specifies the limit in bytes up to which memory allocations should
  5030.      be classified as small allocations for profiling purposes.  This
  5031.      limit must be greater than zero but less than the medium and large
  5032.      bounds.  Default value: `SMALLBOUND=32'.
  5033.  
  5034. `UNFREEDABORT'=<UNSIGNED-INTEGER>
  5035.      Specifies the minimum number of unfreed allocations at which to
  5036.      abort the program just before program termination.  A summary of
  5037.      all the allocations will be displayed on the standard error file
  5038.      stream before aborting.  This option may be handy for use in batch
  5039.      tests as it can force tests to fail if they do not free up a
  5040.      minimum number of memory allocations.  Note that this setting will
  5041.      be ignored if its value is zero.  Default value: `UNFREEDABORT=0'.
  5042.  
  5043. `USEDEBUG'
  5044.      Specifies that any debugging information in the executable file
  5045.      should be used to obtain additional source-level information.
  5046.      This option will only have an effect if the executable file
  5047.      contains a compiler-generated line number table and will be
  5048.      ignored if the mpatrol library was built to support an object file
  5049.      access library that cannot read line tables from object files.
  5050.      Note that this option will slow down program execution, use up
  5051.      more system memory and may leave unaccounted unfreed memory
  5052.      allocations at program termination.
  5053.  
  5054. `USEMMAP'
  5055.      Specifies that the library should use `mmap()' instead of `sbrk()'
  5056.      to allocate system memory on UNIX platforms.  This option should
  5057.      be used if there are problems when using the mpatrol library in
  5058.      combination with another malloc library which uses `sbrk()' to
  5059.      allocate its memory.  It is ignored on systems that do not support
  5060.      the `mmap()' system call.
  5061.  
  5062. 
  5063. File: mpatrol.info,  Node: Options,  Next: Library performance,  Prev: Environment,  Up: Top
  5064.  
  5065. Options
  5066. *******
  5067.  
  5068.    A utility program called `mpatrol' is provided to run commands that
  5069. have been linked with the mpatrol library.
  5070.  
  5071.      mpatrol [options] <command> [arguments]
  5072.  
  5073.    The `mpatrol' command is used to set various mpatrol library OPTIONS
  5074. when running COMMAND with its ARGUMENTS.  In most cases, COMMAND must
  5075. have been linked with the mpatrol library, unless the `-d' option is
  5076. used in which case COMMAND need only have been dynamically linked.
  5077.  
  5078.    All mpatrol library diagnostics are sent to the file
  5079. `mpatrol.%n.log' in the current directory by default (where `%n' is the
  5080. current process id) but this can be changed using the `-l' option.
  5081. Similarly, the default profiling output filename is `mpatrol.%n.out'.
  5082. Note that the `LOGALL' option is always implicitly used for commands
  5083. that are run by this command.
  5084.  
  5085.    Alternatively, the log file and profiling output file names can
  5086. contain `%p', which will be replaced with the name of the program being
  5087. executed without the directory components.  If the executable filename
  5088. could not be determined or was not set then it will be replaced with
  5089. `mpatrol'.
  5090.  
  5091.    All of the following options (except `-d' and `-V') correspond to
  5092. their listed mpatrol library option (*note Environment::).
  5093.  
  5094. `-1' <UNSIGNED-INTEGER>
  5095.      [`SMALLBOUND']  Specifies the limit in bytes up to which memory
  5096.      allocations should be classified as small allocations for
  5097.      profiling purposes.
  5098.  
  5099. `-2' <UNSIGNED-INTEGER>
  5100.      [`MEDIUMBOUND']  Specifies the limit in bytes up to which memory
  5101.      allocations should be classified as medium allocations for
  5102.      profiling purposes.
  5103.  
  5104. `-3' <UNSIGNED-INTEGER>
  5105.      [`LARGEBOUND']  Specifies the limit in bytes up to which memory
  5106.      allocations should be classified as large allocations for
  5107.      profiling purposes.
  5108.  
  5109. `-A' <UNSIGNED-INTEGER>
  5110.      [`ALLOCSTOP']  Specifies an allocation index at which to stop the
  5111.      program when it is being allocated.
  5112.  
  5113. `-a' <UNSIGNED-INTEGER>
  5114.      [`ALLOCBYTE']  Specifies an 8-bit byte pattern with which to
  5115.      prefill newly-allocated memory.
  5116.  
  5117. `-C' <UNSIGNED-RANGE>
  5118.      [`CHECK']  Specifies a range of allocation indices at which to
  5119.      check the integrity of free memory and overflow buffers.
  5120.  
  5121. `-c'
  5122.      [`CHECKALL']  Specifies that all arguments to functions which
  5123.      allocate, reallocate and deallocate memory have rigorous checks
  5124.      performed on them.
  5125.  
  5126. `-D' <UNSIGNED-INTEGER>
  5127.      [`DEFALIGN']  Specifies the default alignment for general-purpose
  5128.      memory allocations, which must be a power of two.
  5129.  
  5130. `-d'
  5131.      Specifies that programs which were not linked with the mpatrol
  5132.      library should also be traced, but only if they were dynamically
  5133.      linked.  This option will only work if the system dynamic linker
  5134.      has the ability to preload a set of user-specified shared
  5135.      libraries via a special environment variable.
  5136.  
  5137. `-e' <STRING>
  5138.      [`PROGFILE']  Specifies an alternative filename with which to
  5139.      locate the executable file containing the program's symbols.
  5140.  
  5141. `-F' <UNSIGNED-INTEGER>
  5142.      [`FREESTOP']  Specifies an allocation index at which to stop the
  5143.      program when it is being freed.
  5144.  
  5145. `-f' <UNSIGNED-INTEGER>
  5146.      [`FREEBYTE']  Specifies an 8-bit byte pattern with which to prefill
  5147.      newly-freed memory.
  5148.  
  5149. `-G'
  5150.      [`SAFESIGNALS']  Instructs the library to save and replace certain
  5151.      signal handlers during the execution of library code and to
  5152.      restore them afterwards.
  5153.  
  5154. `-g'
  5155.      [`USEDEBUG']  Specifies that any debugging information in the
  5156.      executable file should be used to obtain additional source-level
  5157.      information.
  5158.  
  5159. `-L' <UNSIGNED-INTEGER>
  5160.      [`LIMIT']  Specifies the limit in bytes at which all memory
  5161.      allocations should fail if the total allocated memory should
  5162.      increase beyond this.
  5163.  
  5164. `-l' <STRING>
  5165.      [`LOGFILE']  Specifies an alternative file in which to place all
  5166.      diagnostics from the mpatrol library.
  5167.  
  5168. `-M'
  5169.      [`ALLOWOFLOW']  Specifies that a warning rather than an error
  5170.      should be produced if any memory operation function overflows the
  5171.      boundaries of a memory allocation, and that the operation should
  5172.      still be performed.
  5173.  
  5174. `-m'
  5175.      [`USEMMAP']  Specifies that the library should use `mmap()' instead
  5176.      of `sbrk()' to allocate system memory.
  5177.  
  5178. `-N'
  5179.      [`NOPROTECT']  Specifies that the mpatrol library's internal data
  5180.      structures should not be made read-only after every memory
  5181.      allocation, reallocation or deallocation.
  5182.  
  5183. `-n'
  5184.      [`NOFREE']  Specifies that the mpatrol library should keep all
  5185.      reallocated and freed memory allocations.
  5186.  
  5187. `-O' <UNSIGNED-INTEGER>
  5188.      [`OFLOWSIZE']  Specifies the size in bytes to use for all overflow
  5189.      buffers, which must be a power of two.
  5190.  
  5191. `-o' <UNSIGNED-INTEGER>
  5192.      [`OFLOWBYTE']  Specifies an 8-bit byte pattern with which to fill
  5193.      the overflow buffers of all memory allocations.
  5194.  
  5195. `-P' <STRING>
  5196.      [`PROFFILE']  Specifies an alternative file in which to place all
  5197.      memory allocation profiling information from the mpatrol library.
  5198.  
  5199. `-p'
  5200.      [`PROF']  Specifies that all memory allocations are to be profiled
  5201.      and sent to the profiling output file.
  5202.  
  5203. `-Q' <UNSIGNED-INTEGER>
  5204.      [`AUTOSAVE']  Specifies the frequency at which to periodically
  5205.      write the profiling data to the profiling output file.
  5206.  
  5207. `-R' <UNSIGNED-INTEGER>
  5208.      [`REALLOCSTOP']  Specifies an allocation index at which to stop the
  5209.      program when a memory allocation is being reallocated.
  5210.  
  5211. `-S'
  5212.      [`SHOWMAP' & `SHOWSYMBOLS']  Specifies that a memory map of the
  5213.      entire heap and a summary of all of the function symbols read from
  5214.      the program's executable file should be displayed at the end of
  5215.      program execution.
  5216.  
  5217. `-s'
  5218.      [`SHOWFREED' & `SHOWUNFREED']  Specifies that a summary of all of
  5219.      the freed and unfreed memory allocations should be displayed at
  5220.      the end of program execution.
  5221.  
  5222. `-U' <UNSIGNED-INTEGER>
  5223.      [`UNFREEDABORT']  Specifies the minimum number of unfreed
  5224.      allocations at which to abort the program just before program
  5225.      termination.
  5226.  
  5227. `-V'
  5228.      Displays the version number of the `mpatrol' command.
  5229.  
  5230. `-v'
  5231.      [`PRESERVE']  Specifies that any reallocated or freed memory
  5232.      allocations should preserve their original contents.
  5233.  
  5234. `-w'
  5235.      [`OFLOWWATCH']  Specifies that watch point areas should be used for
  5236.      overflow buffers rather than filling with the overflow byte.
  5237.  
  5238. `-X'
  5239.      [`PAGEALLOC=UPPER']  Specifies that each individual memory
  5240.      allocation should occupy at least one page of virtual memory and
  5241.      should be placed at the highest point within these pages.
  5242.  
  5243. `-x'
  5244.      [`PAGEALLOC=LOWER']  Specifies that each individual memory
  5245.      allocation should occupy at least one page of virtual memory and
  5246.      should be placed at the lowest point within these pages.
  5247.  
  5248. `-Z' <UNSIGNED-INTEGER>
  5249.      [`FAILSEED']  Specifies the random number seed which will be used
  5250.      when determining which memory allocations will randomly fail.
  5251.  
  5252. `-z' <UNSIGNED-INTEGER>
  5253.      [`FAILFREQ']  Specifies the frequency at which all memory
  5254.      allocations will randomly fail.
  5255.  
  5256. 
  5257. File: mpatrol.info,  Node: Library performance,  Next: Profiling file format,  Prev: Options,  Up: Top
  5258.  
  5259. Library performance
  5260. *******************
  5261.  
  5262.    The following times were obtained on a Sun Ultra 5 with an
  5263. UltraSPARC IIi processor running at 333MHz and running Solaris 7.  The
  5264. test performed was the one in `tests/pass/test1.c' and all tests were
  5265. run on a lightly loaded system, but were run several times to obtain an
  5266. average result.  Obviously, these times can only be an approximation,
  5267. but should serve to illustrate the effects on performance that each
  5268. option can have.  All times are given in seconds, and the second time
  5269. on each line was obtained with the same options plus the `NOPROTECT'
  5270. option.  Running with the `CHECK=0' option would speed things up
  5271. dramatically, albeit at the expense of less error checking.
  5272.  
  5273.    Running with basic options:
  5274.  
  5275. _no options_                                0.618       0.258
  5276. `OFLOWSIZE=2'                               0.645       0.296
  5277. `OFLOWSIZE=8'                               0.686       0.327
  5278. `PAGEALLOC=LOWER'                           7.785       7.372
  5279. `PAGEALLOC=UPPER'                           7.821       7.469
  5280.  
  5281.    Running when all freed memory allocations are kept:
  5282.  
  5283. `NOFREE'                                    0.943       0.506
  5284. `NOFREE OFLOWSIZE=2'                        1.026       0.579
  5285. `NOFREE OFLOWSIZE=8'                        1.091       0.645
  5286. `NOFREE PAGEALLOC=LOWER'                    8.013       7.598
  5287. `NOFREE PAGEALLOC=UPPER'                    8.026       7.616
  5288.  
  5289.    Running when all freed memory allocations are kept and their
  5290. contents are preserved:
  5291.  
  5292. `NOFREE PRESERVE'                           0.719       0.292
  5293. `NOFREE PRESERVE OFLOWSIZE=2'               0.792       0.367
  5294. `NOFREE PRESERVE OFLOWSIZE=8'               0.850       0.419
  5295. `NOFREE PRESERVE PAGEALLOC=LOWER'           8.043       7.616
  5296. `NOFREE PRESERVE PAGEALLOC=UPPER'           8.052       7.631
  5297.  
  5298.    Running using watch points to check the overflow buffers:
  5299.  
  5300. `OFLOWSIZE=2 OFLOWWATCH'                    Interrupted after half an
  5301.                                             hour as it still hadn't
  5302.                                             finished.
  5303.  
  5304.    Running using the Solaris 7 malloc libraries:
  5305.  
  5306. Solaris 7 malloc(3c) library                0.033
  5307. Solaris 7 malloc(3x) library                0.036
  5308. Solaris 7 bsdmalloc(3x) library             0.028
  5309. Solaris 7 mapmalloc(3x) library             0.033
  5310. Solaris 7 watchmalloc(3x) library           40.845
  5311.  
  5312. 
  5313. File: mpatrol.info,  Node: Profiling file format,  Next: Supported systems,  Prev: Library performance,  Up: Top
  5314.  
  5315. Profiling file format
  5316. *********************
  5317.  
  5318.    The format of the profiling output files that are produced by the
  5319. mpatrol library is described here.  Every profiling output file
  5320. contains the following components.
  5321.  
  5322.    * 4 bytes containing the characters `M', `P', `T' and `L'.
  5323.  
  5324.    * 1 unsigned integer representing the value `1'.  This is used by
  5325.      `mprof' to determine the endianness of the processor that produced
  5326.      the profiling output file so that it can decide whether to perform
  5327.      byte-swapping on the input data.
  5328.  
  5329.    * 3 unsigned integers containing the small, medium and large
  5330.      allocation bounds.
  5331.  
  5332.    * 1 unsigned integer containing the allocation bin size.  If the
  5333.      allocation bin size is greater than zero then it is followed by
  5334.      the allocation bins, the large allocation totals, the deallocation
  5335.      bins and the large deallocation totals, where the bins are arrays
  5336.      of unsigned integers with dimensions of the allocation bin size
  5337.      and the totals are unsigned integers.
  5338.  
  5339.    * 1 unsigned integer containing the number of profiling data
  5340.      structures.  If the number of profiling data structures is greater
  5341.      than zero then it is followed by the profiling data structures
  5342.      themselves, which are of the following structure.
  5343.  
  5344.         * 1 unsigned integer representing the index of this profiling
  5345.           data.
  5346.  
  5347.         * 4 unsigned integers representing the small, medium, large and
  5348.           extra large allocation counts for this profiling data.
  5349.  
  5350.         * 4 unsigned integers representing the small, medium, large and
  5351.           extra large allocation totals for this profiling data.
  5352.  
  5353.         * 4 unsigned integers representing the small, medium, large and
  5354.           extra large deallocation counts for this profiling data.
  5355.  
  5356.         * 4 unsigned integers representing the small, medium, large and
  5357.           extra large deallocation totals for this profiling data.
  5358.  
  5359.    * 1 unsigned integer containing the number of call sites.  If the
  5360.      number of call sites is greater than zero then it is followed by
  5361.      the call sites themselves, which are of the following structure.
  5362.  
  5363.         * 1 unsigned integer representing the index of this call site.
  5364.  
  5365.         * 1 unsigned integer representing the index of the parent call
  5366.           site.
  5367.  
  5368.         * 1 generic pointer representing the code address of this call
  5369.           site.
  5370.  
  5371.         * 1 unsigned integer representing the index of an associated
  5372.           symbol.
  5373.  
  5374.         * 1 unsigned integer representing the offset of the symbol name.
  5375.  
  5376.         * 1 unsigned integer representing the index of any associated
  5377.           profiling data.
  5378.  
  5379.    * 1 unsigned integer containing the number of symbol addresses.  If
  5380.      the number of symbol addresses is greater than zero then it is
  5381.      followed by the symbol addresses themselves, which are generic
  5382.      pointers.
  5383.  
  5384.    * 1 unsigned integer containing the size of the symbol name string
  5385.      table.  This is followed by the symbol name string table, which is
  5386.      an array of characters containing the nul-terminated symbol names.
  5387.  
  5388.    * 4 bytes containing the characters `M', `P', `T' and `L'.
  5389.  
  5390. 
  5391. File: mpatrol.info,  Node: Supported systems,  Next: Notes,  Prev: Profiling file format,  Up: Top
  5392.  
  5393. Supported systems
  5394. *****************
  5395.  
  5396.    Following is a list of systems on which the mpatrol library has been
  5397. built and tested.  The system details include the operating system and
  5398. version, the processor type, the object file format and the C compiler
  5399. used to compile the library and tests.  The details following each
  5400. system list any features of the library that are not (or cannot be)
  5401. supported on that system.
  5402.  
  5403.    * AIX 4.1, IBM RS/6000, BFD, `cc'
  5404.         * The thread-safe version of the library does not work.
  5405.  
  5406.         * The `OFLOWWATCH' option has no effect.
  5407.  
  5408.         * Cannot automatically determine the program filename.
  5409.  
  5410.         * No support for call stack traversal.
  5411.  
  5412.         * The address of an illegal memory access cannot be determined.
  5413.  
  5414.         * The `-d' option to the `mpatrol' command has no effect.
  5415.  
  5416.    * DG/UX 4.20MU07, Intel Pentium Pro, ELF32, `gcc'
  5417.         * The thread-safe version of the library does not work.
  5418.  
  5419.         * The `OFLOWWATCH' option has no effect.
  5420.  
  5421.         * The `USEDEBUG' option has no effect.
  5422.  
  5423.         * The `-d' option to the `mpatrol' command does not work unless
  5424.           `libelf.so' is available.
  5425.  
  5426.    * DG/UX 4.11, Intel Pentium Pro, ELF32, `gcc'
  5427.         * The thread-safe version of the library does not work.
  5428.  
  5429.         * The `OFLOWWATCH' option has no effect.
  5430.  
  5431.         * The `USEDEBUG' option has no effect.
  5432.  
  5433.         * The `-d' option to the `mpatrol' command has no effect.
  5434.  
  5435.    * DG/UX 4.11, Motorola 88100, ELF32, `gcc'
  5436.         * The thread-safe version of the library does not work.
  5437.  
  5438.         * The `OFLOWWATCH' option has no effect.
  5439.  
  5440.         * The `USEDEBUG' option has no effect.
  5441.  
  5442.         * Cannot automatically determine the program filename.
  5443.  
  5444.         * Call stack traversal only works with unoptimised code.
  5445.  
  5446.         * The `-d' option to the `mpatrol' command has no effect.
  5447.  
  5448.    * DYNIX/ptx 4.5, Intel Pentium Pro, ELF32, `cc'
  5449.         * The thread-safe version of the library does not work.
  5450.  
  5451.         * The `OFLOWWATCH' option has no effect.
  5452.  
  5453.         * The `USEDEBUG' option has no effect.
  5454.  
  5455.         * The `-d' option to the `mpatrol' command has no effect.
  5456.  
  5457.    * HP/UX 10.20, HP PA/RISC 9000, BFD, `gcc'
  5458.         * The thread-safe version of the library does not work.
  5459.  
  5460.         * The `OFLOWWATCH' option has no effect.
  5461.  
  5462.         * The `USEMMAP' option has no effect.
  5463.  
  5464.         * Cannot automatically determine the program filename.
  5465.  
  5466.         * No support for call stack traversal.
  5467.  
  5468.         * The `-d' option to the `mpatrol' command has no effect.
  5469.  
  5470.    * IRIX 5.3, MIPS R4000, ELF32, `cc'
  5471.         * The thread-safe version of the library does not work.
  5472.  
  5473.         * The `OFLOWWATCH' option has no effect.
  5474.  
  5475.         * The `USEDEBUG' option has no effect.
  5476.  
  5477.         * No support for call stack traversal.
  5478.  
  5479.         * Symbols cannot be read from any shared libraries that a
  5480.           program uses.
  5481.  
  5482.    * RedHat Linux 6.0, Intel Pentium III, BFD, `gcc'
  5483.         * The thread-safe version of the library does not work.
  5484.  
  5485.         * The `OFLOWWATCH' option has no effect.
  5486.  
  5487.         * The address of an illegal memory access cannot be determined.
  5488.  
  5489.         * The `-d' option to the `mpatrol' command does not work unless
  5490.           `libiberty.so' is available.
  5491.  
  5492.    * RedHat Linux 5.1, Motorola 68040, BFD, `gcc'
  5493.         * The thread-safe version of the library does not work.
  5494.  
  5495.         * The `OFLOWWATCH' option has no effect.
  5496.  
  5497.         * The address of an illegal memory access cannot be determined.
  5498.  
  5499.         * The `-d' option to the `mpatrol' command does not work unless
  5500.           `libiberty.so' is available.
  5501.  
  5502.    * RedHat Linux 5.1, Motorola 68040, ELF32, `gcc'
  5503.         * The thread-safe version of the library does not work.
  5504.  
  5505.         * The `OFLOWWATCH' option has no effect.
  5506.  
  5507.         * The `USEDEBUG' option has no effect.
  5508.  
  5509.         * The address of an illegal memory access cannot be determined.
  5510.  
  5511.         * The `-d' option to the `mpatrol' command does not work unless
  5512.           `libelf.so' is available.
  5513.  
  5514.    * LynxOS 3.0.0, PowerPC, BFD, `gcc'
  5515.         * The thread-safe version of the library does not work.
  5516.  
  5517.         * The `OFLOWWATCH' option has no effect.
  5518.  
  5519.         * The `USEMMAP' option has no effect.
  5520.  
  5521.         * Cannot automatically determine the program filename.
  5522.  
  5523.         * No support for call stack traversal.
  5524.  
  5525.         * The address of an illegal memory access cannot be determined.
  5526.  
  5527.         * The `-d' option to the `mpatrol' command has no effect.
  5528.  
  5529.    * Solaris 2.6, Intel Pentium Pro, BFD, `gcc'
  5530.         * The thread-safe version of the library does not work.
  5531.  
  5532.    * Solaris 2.6, Intel Pentium Pro, ELF32, `gcc'
  5533.         * The thread-safe version of the library does not work.
  5534.  
  5535.         * The `USEDEBUG' option has no effect.
  5536.  
  5537.    * Solaris 7, SPARC V9, BFD, `gcc'
  5538.         * The thread-safe version of the library does not work.
  5539.  
  5540.    * Solaris 7, SPARC V9, ELF32, `gcc'
  5541.         * The thread-safe version of the library does not work.
  5542.  
  5543.         * The `USEDEBUG' option has no effect.
  5544.  
  5545.    * AmigaOS 3.1, Motorola 68040, BFD, `gcc'
  5546.         * No memory protection so the `PAGEALLOC' option has no effect.
  5547.  
  5548.         * The `OFLOWWATCH' option has no effect.
  5549.  
  5550.         * The `USEDEBUG' option has no effect.
  5551.  
  5552.         * The `USEMMAP' option has no effect.
  5553.  
  5554.         * Limited support for call stack traversal.
  5555.  
  5556.         * Limited support for reading symbols.
  5557.  
  5558.         * No detection of illegal memory accesses.
  5559.  
  5560.         * The `-d' option to the `mpatrol' command has no effect.
  5561.  
  5562.    * AmigaOS 3.1, Motorola 68040, n/a, SAS/C
  5563.         * No automatic override of `malloc()', etc., without inclusion
  5564.           of `mpatrol.h'.
  5565.  
  5566.         * No memory protection so the `PAGEALLOC' option has no effect.
  5567.  
  5568.         * The `OFLOWWATCH' option has no effect.
  5569.  
  5570.         * The `USEDEBUG' option has no effect.
  5571.  
  5572.         * The `USEMMAP' option has no effect.
  5573.  
  5574.         * No support for call stack traversal.
  5575.  
  5576.         * No support for reading symbols.
  5577.  
  5578.         * No detection of illegal memory accesses.
  5579.  
  5580.         * The `-d' option to the `mpatrol' command has no effect.
  5581.  
  5582.    * Microsoft Windows NT 4.0, Intel Pentium III, n/a, Microsoft Visual
  5583.      C/C++
  5584.         * The `OFLOWWATCH' option has no effect.
  5585.  
  5586.         * The `USEDEBUG' option has no effect.
  5587.  
  5588.         * The `USEMMAP' option has no effect.
  5589.  
  5590.         * No support for reading symbols.
  5591.  
  5592.         * The address of an illegal memory access cannot be determined.
  5593.  
  5594.         * The `-d' option to the `mpatrol' command has no effect.
  5595.  
  5596. * Menu:
  5597.  
  5598. * New system support::          Adding a new operating system.
  5599. * New processor support::       Adding a new processor architecture.
  5600. * New file format support::     Adding a new object file format.
  5601.  
  5602. 
  5603. File: mpatrol.info,  Node: New system support,  Next: New processor support,  Up: Supported systems
  5604.  
  5605. Adding a new operating system
  5606. =============================
  5607.  
  5608.    * Add a new `TARGET' and/or `SYSTEM' definition in `target.h'.  The
  5609.      `TARGET' macro is for fundamentally different operating systems,
  5610.      whereas the `SYSTEM' macro is for differentiating variations of a
  5611.      particular operating system.
  5612.  
  5613.    * Make any necessary modifications to `config.h'.
  5614.  
  5615.    * Add any support for memory allocation in `memory.c'.
  5616.  
  5617.    * Add any support for stack traversal in `stack.c'.
  5618.  
  5619.    * Add any support for signals in `signals.c'.
  5620.  
  5621.    * Add any support for threads in `mutex.c'.
  5622.  
  5623.    * Add any support for filenames in `diag.c'.
  5624.  
  5625.    * Add a new version and date format (or use an existing one) in
  5626.      `version.c'.
  5627.  
  5628.    * Decide if the `malloc()' replacements should be used from
  5629.      `malloc.c'.
  5630.  
  5631.    * Add any support for invoking commands in `mpatrol.c'.
  5632.  
  5633.    * Add a new subdirectory in the `build' directory that contains a
  5634.      `Makefile' and any other files that are required to build the
  5635.      library on the new operating system.
  5636.  
  5637. 
  5638. File: mpatrol.info,  Node: New processor support,  Next: New file format support,  Prev: New system support,  Up: Supported systems
  5639.  
  5640. Adding a new processor architecture
  5641. ===================================
  5642.  
  5643.    * Add a new `ARCH' definition in `target.h'.
  5644.  
  5645.    * Make any necessary modifications to `config.h'.
  5646.  
  5647.    * Add any support for memory allocation in `memory.c'.
  5648.  
  5649.    * Add any support for stack traversal in `stack.c'.
  5650.  
  5651. 
  5652. File: mpatrol.info,  Node: New file format support,  Prev: New processor support,  Up: Supported systems
  5653.  
  5654. Adding a new object file format
  5655. ===============================
  5656.  
  5657.    * Add a new `FORMAT' definition in `target.h'.
  5658.  
  5659.    * Make any necessary modifications to `config.h'.
  5660.  
  5661.    * Add any support for stack traversal in `stack.c'.
  5662.  
  5663.    * Add any support for symbol reading in `symbol.c'.
  5664.  
  5665. 
  5666. File: mpatrol.info,  Node: Notes,  Next: Related software,  Prev: Supported systems,  Up: Top
  5667.  
  5668. Notes
  5669. *****
  5670.  
  5671.    This section contains information about known bugs and limitations
  5672. in the mpatrol library as well as listing potential future enhancements.
  5673.  
  5674.    Bugs should be reported to <mpatrol@cbmamiga.demon.co.uk> along with
  5675. the details of the operating system, processor architecture and object
  5676. file format that the mpatrol library is being used with -- and don't
  5677. forget to include the version of the mpatrol library you are using!
  5678. Keep in mind that I only have access to an Amiga running RedHat
  5679. Linux/m68k 5.1 and AmigaOS 3.1, so I will be most likely unable to
  5680. reproduce most of the system-specific bugs.  A bug report that comes
  5681. with an associated fix will be most welcome.
  5682.  
  5683.    Enhancement requests and source code containing enhancements should
  5684. also be sent to <mpatrol@cbmamiga.demon.co.uk> or the mpatrol
  5685. discussion group at `http://www.egroups.com/group/mpatrol/'.  If you
  5686. are planning to implement an enhancement, let me know first in case I
  5687. am (or someone else is) working towards the same goal -- that way, work
  5688. won't be wasted.  If you wish to send me source code changes please
  5689. send the changes as context diffs or in an e-mail attachment as a
  5690. compressed tar archive.
  5691.  
  5692. * Menu:
  5693.  
  5694. * Generic notes::               Notes for all platforms.
  5695. * UNIX notes::                  Notes for UNIX platforms.
  5696. * Amiga notes::                 Notes for Amiga platforms.
  5697. * Windows notes::               Notes for Windows platforms.
  5698. * Netware notes::               Notes for Netware platforms.
  5699.  
  5700. 
  5701. File: mpatrol.info,  Node: Generic notes,  Next: UNIX notes,  Up: Notes
  5702.  
  5703. Notes for all platforms
  5704. =======================
  5705.  
  5706.    * C++ support is still fairly limited, and will possibly only work
  5707.      for older C++ code due to the way the operators are overridden
  5708.      (i.e. there are no exceptions versions of the functions).  There
  5709.      are also likely to be potential problems with the macros which
  5710.      redefine `malloc()' and `operator new', etc., since there may be
  5711.      member functions in code that will mistakenly be redefined if their
  5712.      names match the macro definitions, and also means that calls to
  5713.      placement `new' will not work at all.  Also, explicit references to
  5714.      `operator new' rather than `new' are likely to result in
  5715.      compilation errors, and the way that source level information is
  5716.      obtained for `operator delete' means that the resulting code will
  5717.      not be thread-safe.
  5718.  
  5719.    * Need to add support for 64-bit processors.  This shouldn't be too
  5720.      hard, but I haven't got access to a 64-bit environment to test it,
  5721.      so I haven't bothered yet.
  5722.  
  5723.    * The thread-safe code in the library doesn't yet work properly,
  5724.      probably because of the recursion flag which is incremented or
  5725.      decremented before the mutex is locked.  Hence, the threads test
  5726.      (`tests/pass/test5.c') doesn't work yet.
  5727.  
  5728.    * Need to make the library re-entrant.  This could be achieved by
  5729.      moving the static variables in `memory.c', `stack.c', `mutex.c',
  5730.      `diag.c', `option.c' and `sbrk.c' into the `infohead' structure
  5731.      and then having an array of `infohead' structures from which to
  5732.      allocate new memory headers when a new one is required.  This is
  5733.      only necessary for Amiga shared libraries and Netware NLMs since
  5734.      UNIX and Windows platforms allocate a new copy of the data section
  5735.      in a shared library or DLL when it is opened by a new process.
  5736.  
  5737.    * The current implementation of call stack traversal is limited and
  5738.      will only likely work for unoptimised code.  A much better
  5739.      solution would be write the implementation at a lower level in
  5740.      assembly, but this is much less portable.  Perhaps there is a
  5741.      library which can be used to perform this across many operating
  5742.      systems and processor architectures, or maybe someone would like to
  5743.      write one?  I can think of many applications that would benefit
  5744.      from such a library besides this one(1).
  5745.  
  5746.    * An alternative implementation for call stack traversal uses the
  5747.      functions `__builtin_frame_address()' and
  5748.      `__builtin_return_address()' that are available when the library
  5749.      is compiled with `gcc'.  However, they can only traverse a number
  5750.      of stack frames at compile-time, not run-time so there is a
  5751.      maximum number of stack frames that can be traversed at any one
  5752.      time.  The implementation depends on both of these builtin
  5753.      functions returning `NULL' when the top of stack is reached.  If
  5754.      this is not the case then this method cannot be used or should
  5755.      only be used with a small number of fixed stack frames.
  5756.  
  5757.    * Add a function to display a stack trace of the current call stack
  5758.      to a file stream and another function to walk all current memory
  5759.      allocations invoking a call-back function for each one
  5760.      encountered.  Note that there is still an issue with call-back
  5761.      functions if they call mpatrol library functions, since this will
  5762.      lead to recursion.
  5763.  
  5764.    * In object file formats that support nested symbols (such as ELF),
  5765.      the current implementation will tend to show some shortcomings.
  5766.      This is because there is currently no nesting count in the
  5767.      function that deals with symbol name lookup, so the wrong symbol
  5768.      name may be displayed in diagnostics.
  5769.  
  5770.    * In object file formats that don't store the sizes of symbols (such
  5771.      as basic COFF, or when using the GNU BFD library), the current
  5772.      implementation will simply assume that the current symbol
  5773.      terminates at the beginning of the next symbol in the virtual
  5774.      address space.
  5775.  
  5776.    * There still appears to be a problem with the code address to line
  5777.      number mapping provided by the GNU BFD library, and this needs to
  5778.      be looked into further.  Also need to store filename and line
  5779.      number information in all call stacks so that the information can
  5780.      be used at program termination.  May also need to display this
  5781.      information in the `__mp_printinfo()' function and add this
  5782.      information to the profiling output file so that `mprof' can make
  5783.      use of it.
  5784.  
  5785.    * Perhaps add the ability to profile memory operations such as
  5786.      `memcpy()' and `memset()' to the existing memory allocation
  5787.      profiling facility.  Also, add options to `mprof' to write out
  5788.      files that can be used by graph drawing software for a better
  5789.      visualisation of the profiling information.  Finally, perhaps add
  5790.      an allocation call graph table to `mprof', similar to that
  5791.      produced by `gprof' for execution call graphs.
  5792.  
  5793.    * Perhaps add a memory usage profiling feature which would dump out
  5794.      memory usage statistics to a file at set intervals during run-time
  5795.      so that a table of memory usage could be built up.  This could
  5796.      also be extended to having a graphical tool which could display
  5797.      this information.
  5798.  
  5799.    * Add a CRC checksum to memory blocks and use it to check that freed
  5800.      memory allocations have not been corrupted when the `NOFREE' and
  5801.      `PRESERVE' options are in use on platforms which have no memory
  5802.      protection.  This could also be extended to marking allocated
  5803.      memory blocks and then displaying what blocks have changed after a
  5804.      certain period from within a debugger.  Another idea could be to
  5805.      display all memory allocations, etc.  made since a certain
  5806.      function was called from within a program.
  5807.  
  5808.    * Improve use of watch points by allowing an option which will only
  5809.      install write watch points instead of both read and write watch
  5810.      points.  Not only will this speed up the use of watch points, but
  5811.      will also cause less problems with reading from misaligned memory
  5812.      allocations.
  5813.  
  5814.    * Perhaps add memory protection to the simulated `sbrk' heap.
  5815.  
  5816.    * Add a `SHOWFREE' option to display a list of all free memory
  5817.      blocks at program termination for debugging purposes to view
  5818.      memory fragmentation.  If that option is added then perhaps
  5819.      `SHOWALL' should only be equivalent to `SHOWFREE', `SHOWFREED' and
  5820.      `SHOWUNFREED', and `SHOWMAP' and `SHOWSYMBOLS' should be
  5821.      explicitly given.
  5822.  
  5823.    * Add an option, similar to `NOFREE', that would prevent a freed
  5824.      memory allocation from being used until a certain number of memory
  5825.      allocations later.  This would be far less of a resource-hogger
  5826.      than the `NOFREE' option and might catch just as many errors but
  5827.      might be extremely hard to implement.
  5828.  
  5829.    * Add an extra piece of information in the log file summary which
  5830.      highlights the peak number of memory allocations in use at any one
  5831.      time during program execution.
  5832.  
  5833.    * Add versions of `mallopt()', `mallinfo()', `memorymap()',
  5834.      `mallocctl()', `mallocblksize()' and `msize()' which are provided
  5835.      in many other malloc libraries.  These won't necessarily behave in
  5836.      exactly the same way as existing implementations, but at least
  5837.      there won't be link errors when compiling source code which uses
  5838.      them.
  5839.  
  5840.    * Perhaps add debugging/tracing versions of the string manipulation
  5841.      functions, such as `strlen()' and `strcmp()' in much the same way
  5842.      as was done for the memory operation functions.  The only problem
  5843.      with this would be locale support, but perhaps it might be easier
  5844.      just to assume the C locale to begin with.  Also need to have
  5845.      better detection of internal and free blocks when displaying
  5846.      memory range errors.
  5847.  
  5848.    * Perhaps add definitions of `xmalloc()', `xrealloc()', etc. which
  5849.      never return `NULL' on failure, and perhaps also add definitions of
  5850.      `XtMalloc()', `XtRealloc()', etc. for X-Window programming.  Some
  5851.      other malloc libraries provide versions of these but perhaps they
  5852.      are not needed if they are implemented using `malloc()',
  5853.      `realloc()', etc.
  5854.  
  5855.    * Add another library which can be linked in instead of mpatrol and
  5856.      replaces all calls to `__mp_alloc()', etc., with the original
  5857.      calls to `malloc()' and related functions.  This would be very
  5858.      useful for quickly removing all mpatrol functionality for perhaps
  5859.      even a release build, and might be useful for implementing
  5860.      functions such as `memalign()' which don't exist on many systems.
  5861.  
  5862.    * Write a set of functions that are compatible with those
  5863.      implemented by Checker, the `gcc' run-time memory access checker.
  5864.      This would allow every memory access to be checked in object files
  5865.      compiled with `gcc', not just pointers into the heap, and would
  5866.      provide error checking as effective as source code
  5867.      instrumentation.  Could also make use of the `etext', `edata' and
  5868.      `end' pointers that are set at run-time on most UNIX systems.
  5869.  
  5870.    * Perhaps remove the automatic logging of memory operations from
  5871.      programs run through the `mpatrol' command, and instead add an
  5872.      option to do it explicitly.
  5873.  
  5874.    * Add an option to specify that all failed memory allocations should
  5875.      abort (or at least give a warning) instead of returning a `NULL'
  5876.      pointer.  Also, perhaps add an option to display the partial
  5877.      contents of freed and unfreed allocations in the mpatrol log file.
  5878.  
  5879.    * Perhaps use GNU autoconf to automatically work out values for
  5880.      `config.h' on the platform it is being built on, and also use
  5881.      automake, libtool and install when building and installing files.
  5882.  
  5883.    * The postscript version of the quick reference card seems to print
  5884.      at an unusual offset on some printers.  Also, the PDF version gets
  5885.      created with 1 inch margins rather than half inch margins which
  5886.      means that it is one inch too large in both dimensions.  Need to
  5887.      figure out what is happening in both cases.
  5888.  
  5889.    ---------- Footnotes ----------
  5890.  
  5891.    (1) The latest release of the GNU C library includes a `backtrace()'
  5892. function which fills in an array of return addresses, but this requires
  5893. the presence of the library and some features of GCC.
  5894.  
  5895. 
  5896. File: mpatrol.info,  Node: UNIX notes,  Next: Amiga notes,  Prev: Generic notes,  Up: Notes
  5897.  
  5898. Notes for UNIX platforms
  5899. ========================
  5900.  
  5901.    * Need to add watch point area support for non-Solaris operating
  5902.      systems.  This may be a case of preventing all heap memory from
  5903.      being accessed and providing a signal handler that is called when
  5904.      a read from or write to such memory triggers a signal.  The
  5905.      handler could then determine if the address is in a watch point,
  5906.      and if it is not it could unprotect the memory and return.
  5907.  
  5908.    * Need to improve watch point facility in order to speed it up by an
  5909.      order of magnitudes.  This will most likely involve removing all
  5910.      watch points when entering the library and replacing them when
  5911.      returning to user code.
  5912.  
  5913.    * Need to add advanced signal information for operating systems that
  5914.      do not support the `siginfo()' system call.  This information is
  5915.      used by the signal handler that handles the `SIGSEGV' signal in
  5916.      order to provide useful information about where an illegal memory
  5917.      access occurred.  However, there is currently a problem in that
  5918.      the call stack displayed from within that handler is not
  5919.      necessarily accurate with respect to the function at the top of
  5920.      the stack.  Also, signal handlers shouldn't technically call I/O
  5921.      functions in case of additional signals being caught so this may
  5922.      need to be improved.
  5923.  
  5924.    * Need to add a portable way of initialising the thread-safe version
  5925.      of the library when it is compiled by a C compiler.  There is
  5926.      already a solution to this problem when it is compiled by a C++
  5927.      compiler, but it may only work if the final program is linked with
  5928.      that specific compiler.
  5929.  
  5930.    * Need to add support for call stack traversal for at least the
  5931.      Alpha, MIPS, PA/RISC, PowerPC and RS/6000 processor architectures.
  5932.      The current implementation of call stack traversal for the
  5933.      Motorola 88xx0 family is also a bit flaky and so should only be
  5934.      used when the library and program are built unoptimised.
  5935.  
  5936.    * Need to add support for obtaining the program name from the stack
  5937.      for at least the Alpha, Motorola 88xx0, non-IRIX MIPS, PA/RISC,
  5938.      PowerPC and RS/6000 processor architectures.  Also need to add
  5939.      support for reading the program symbols from a suitable file in
  5940.      `/proc' for other operating systems that support it.  If there is
  5941.      no support for either of these methods then the `PROGFILE' option
  5942.      can currently be used to specify the program name at run-time.
  5943.  
  5944.    * The library cannot currently read any symbols from shared objects
  5945.      that have been read via `dlopen()'.  Also, on IRIX platforms no
  5946.      symbols can currently be read from any shared libraries that were
  5947.      used by a program.  This is because SGI have a slightly different
  5948.      interface to their dynamic linker that I haven't been able to
  5949.      figure out yet.
  5950.  
  5951.    * There is a problem on later Linux releases where the `_DYNAMIC'
  5952.      symbol is defined in `elf.h', thus resulting in a conflicting
  5953.      definition when compiling `symbol.c'.
  5954.  
  5955.    * The `-d' option to the `mpatrol' command does not always work on
  5956.      systems whose dynamic linkers support the `LD_PRELOAD' or
  5957.      `_RLD_LIST' environment variables.  This is because the object
  5958.      file format access libraries do not exist in shared form on such
  5959.      systems.  There is also likely to be an issue when running with
  5960.      thread-safe libraries.
  5961.  
  5962. 
  5963. File: mpatrol.info,  Node: Amiga notes,  Next: Windows notes,  Prev: UNIX notes,  Up: Notes
  5964.  
  5965. Notes for Amiga platforms
  5966. =========================
  5967.  
  5968.    * Perhaps add support for building mpatrol as an Amiga shared
  5969.      library.  I attempted to do this in a previous release of mpatrol,
  5970.      but it would have involved too many source changes to get working
  5971.      fully.  Perhaps it's not even worth implementing as the archive
  5972.      library works fine.
  5973.  
  5974.    * Need to add proper support for call stack traversal for both the
  5975.      Motorola 680x0 and PowerPC processor architectures.  When `gcc' is
  5976.      being used then up to two stack frames can be traversed, but this
  5977.      should really be extended without requiring
  5978.      `MP_BUILTINSTACK_SUPPORT'.  When SAS/C is being used then there is
  5979.      no support for call stack traversal.
  5980.  
  5981.    * Need to add proper support for reading symbols from Amiga
  5982.      executable files.  When `gcc' is being used then the BFD library
  5983.      routines will be called to determine the symbols from the
  5984.      executable file, but this will only work for objects compiled with
  5985.      `gcc'.  When SAS/C is being used then there is no support for
  5986.      reading symbols from executable files.  Also need to add support
  5987.      for reading symbols from any shared libraries that are required by
  5988.      the program.
  5989.  
  5990.    * Possibly make use of other software such as Enforcer, Mungwall or
  5991.      MuLib in order to provide some form of memory protection.  The
  5992.      features of SegTracker could also be put to good use so that the
  5993.      file and hunk location of entries on the call stack could be
  5994.      determined.
  5995.  
  5996.    * When using SAS/C it is currently not possible to override the
  5997.      definition of `malloc()', etc., without including the `mpatrol.h'
  5998.      header file first.  This is because the compiler startup code and
  5999.      libraries call `malloc()' before everything is set up, and so the
  6000.      library cannot properly initialise itself if the `malloc()' that
  6001.      the startup code finds is the `malloc()' in the mpatrol library.
  6002.      This restriction does not exist when using `gcc'.
  6003.  
  6004. 
  6005. File: mpatrol.info,  Node: Windows notes,  Next: Netware notes,  Prev: Amiga notes,  Up: Notes
  6006.  
  6007. Notes for Windows platforms
  6008. ===========================
  6009.  
  6010.    * Need to add watch point area support, possibly by using guard
  6011.      pages as a basis for an implementation.
  6012.  
  6013.    * Need to add support for reading symbols from Windows executable
  6014.      files.  Also need to add support for reading symbols from any DLLs
  6015.      that are required by the program.  This may be possible in a
  6016.      limited fashion by using the GNU BFD library, but may only work
  6017.      with code compiled with `gcc'.
  6018.  
  6019.    * There seems to be a problem when mixing the archive version of the
  6020.      mpatrol library and the Microsoft C run-time library DLL, and vice
  6021.      versa.  This needs to be looked into, but for the moment, don't
  6022.      mix them.
  6023.  
  6024. 
  6025. File: mpatrol.info,  Node: Netware notes,  Prev: Windows notes,  Up: Notes
  6026.  
  6027. Notes for Netware platforms
  6028. ===========================
  6029.  
  6030.    * The library has not yet been built (let alone tested) on Netware
  6031.      platforms.  The names of the system functions that the library
  6032.      calls for Netware were obtained by looking at Novell's developer
  6033.      documentation, so they may not even compile correctly without
  6034.      modification.
  6035.  
  6036.    * Need to add support for building the mpatrol library as an NLM.
  6037.      This is not currently a high priority requirement as the archive
  6038.      library should suffice for most purposes.
  6039.  
  6040.    * Need to add way to determine when the base of the stack has been
  6041.      reached during call stack traversal, since on Netware every
  6042.      application is really a thread running under one large process.
  6043.  
  6044.    * Need to add support for reading symbols from Netware load modules.
  6045.      Also need to add support for reading symbols from any NLMs that
  6046.      are required by the program.  This may be possible in a limited
  6047.      fashion by using the GNU BFD library, but may only work with code
  6048.      compiled with `gcc'.
  6049.  
  6050.    * Need to investigate if it is safe (or even possible) to override
  6051.      the definitions of `malloc()', etc., without including the
  6052.      `mpatrol.h' header file first.  Currently, non-macro definitions
  6053.      for these functions have been disabled in the Netware version of
  6054.      the library in case they affect other NLMs that are currently
  6055.      running.
  6056.  
  6057. 
  6058. File: mpatrol.info,  Node: Related software,  Next: Function index,  Prev: Notes,  Up: Top
  6059.  
  6060. Related software
  6061. ****************
  6062.  
  6063.    A list of software which helps in debugging dynamic memory
  6064. allocation problems is given below(1).  They all provide some of the
  6065. features that mpatrol contains and you may wish to use one of them to
  6066. solve your problem if you have trouble using mpatrol.  I have only ever
  6067. used Dbmalloc and Electric Fence, so I can't vouch for any of the
  6068. others, although if you have any recommendations feel free to let me
  6069. know so I can add them to this list.  In particular, there seems to be
  6070. a shortage of such programs for Netware platforms.
  6071.  
  6072.    * APurify
  6073.     Author
  6074.           Samuel Devulder (<Samuel.Devulder@info.unicaen.fr>)
  6075.  
  6076.     License
  6077.           Free Software
  6078.  
  6079.     Platforms
  6080.           AmigaOS
  6081.  
  6082.     Location
  6083.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6084.  
  6085.     Overview
  6086.           Instruments an assembler source file to insert code that
  6087.           checks all memory accesses.
  6088.  
  6089.    * BoundsChecker
  6090.     Author
  6091.           NuMega Corporation (<info@numega.com>)
  6092.  
  6093.     License
  6094.           Commercial Software
  6095.  
  6096.     Platforms
  6097.           MS-DOS, Windows
  6098.  
  6099.     Location
  6100.           `http://www.numega.com/'
  6101.  
  6102.     Overview
  6103.           Detects and diagnoses errors in static, stack and heap memory
  6104.           and in memory and resource leaks.
  6105.  
  6106.    * Ccmalloc
  6107.     Author
  6108.           Armin Biere (<armin@ira.uk.de>)
  6109.  
  6110.     License
  6111.           GNU General Public License
  6112.  
  6113.     Platforms
  6114.           Various UNIX
  6115.  
  6116.     Location
  6117.           `http://iseran.ira.uka.de/~armin/ccmalloc/'
  6118.  
  6119.     Overview
  6120.           Can interface with `gdb' to find memory leaks, multiple
  6121.           deallocations and memory corruptions in C or C++ programs.
  6122.  
  6123.    * Chaperon
  6124.     Author
  6125.           John Reiser (<jreiser@BitWagon.com>)
  6126.  
  6127.     License
  6128.           Commercial Software
  6129.  
  6130.     Platforms
  6131.           Linux
  6132.  
  6133.     Location
  6134.           `http://www.BitWagon.com/chaperon.html'
  6135.  
  6136.     Overview
  6137.           Runs existing Intel Linux binary application programs, but
  6138.           checks for and reports bad behaviour in accessing memory.
  6139.  
  6140.    * Checker
  6141.     Author
  6142.           Tristan Gingold (<bug-checker@gnu.org>)
  6143.  
  6144.     License
  6145.           GNU General Public License
  6146.  
  6147.     Platforms
  6148.           Various UNIX
  6149.  
  6150.     Location
  6151.           `http://www.gnu.org/'
  6152.  
  6153.     Overview
  6154.           Detects illegal memory accesses when reading from
  6155.           uninitialised memory, writing to freed memory or outside
  6156.           memory blocks.  Also contains a garbage collector for
  6157.           detecting memory leaks.
  6158.  
  6159.    * CSRI malloc
  6160.     Author
  6161.           Mark Moraes (<moraes@deshaw.com>)
  6162.  
  6163.     License
  6164.           Free Software
  6165.  
  6166.     Platforms
  6167.           Various UNIX
  6168.  
  6169.     Location
  6170.           `ftp://ftp.cs.toronto.edu/pub/moraes/malloc.tar.gz'
  6171.  
  6172.     Overview
  6173.           A library of dynamic memory allocation functions with limited
  6174.           debugging and profiling support and detection of memory
  6175.           leaks.  Also comes with a graphical tool to display a dynamic
  6176.           picture of the heap.
  6177.  
  6178.    * Dbmalloc
  6179.     Author
  6180.           Conor P. Cahill (<cpcahil@virtech.vti.com>)
  6181.  
  6182.     License
  6183.           Free Software
  6184.  
  6185.     Platforms
  6186.           Various UNIX
  6187.  
  6188.     Location
  6189.           `http://www.clark.net/pub/dickey/dbmalloc/dbmalloc.html'
  6190.  
  6191.     Overview
  6192.           Provides replacements for memory management library functions
  6193.           and provides a full set of debugging features which detect
  6194.           memory overruns and other types of misuse.
  6195.  
  6196.    * Debauch
  6197.     Author
  6198.           Jon A. Christopher (<jac8792@tamu.edu>)
  6199.  
  6200.     License
  6201.           GNU General Public License
  6202.  
  6203.     Platforms
  6204.           Linux
  6205.  
  6206.     Location
  6207.           `http://quorum.tamu.edu/jon/gnu/'
  6208.  
  6209.     Overview
  6210.           A memory allocation debugger for C which will detect memory
  6211.           leaks, corrupted memory, stores to freed memory and more.
  6212.  
  6213.    * Debug Heap
  6214.     Author
  6215.           IBM Corporation (<info@ibm.com>)
  6216.  
  6217.     License
  6218.           Commercial Software
  6219.  
  6220.     Platforms
  6221.           IBM AS/400
  6222.  
  6223.     Location
  6224.           `http://www.as400.ibm.com/developer/porting/heapexternal.html'
  6225.  
  6226.     Overview
  6227.           A heap debugging environment with stack traceback for IBM
  6228.           AS/400 servers.
  6229.  
  6230.    * Dmalloc
  6231.     Author
  6232.           Gray Watson (<gray@burger.letters.com>)
  6233.  
  6234.     License
  6235.           Free Software
  6236.  
  6237.     Platforms
  6238.           Various UNIX, MS-DOS, Windows
  6239.  
  6240.     Location
  6241.           `http://www.dmalloc.com/'
  6242.  
  6243.     Overview
  6244.           A drop-in replacement for the system's memory management
  6245.           routines, providing powerful debugging facilities
  6246.           configurable at run-time.  Formerly known as Malloc_Dbg.
  6247.  
  6248.    * Electric Fence
  6249.     Author
  6250.           Bruce Perens (<Bruce@Pixar.com>)
  6251.  
  6252.     License
  6253.           GNU General Public License
  6254.  
  6255.     Platforms
  6256.           Various UNIX
  6257.  
  6258.     Location
  6259.           `ftp://ftp.perens.com/pub/ElectricFence/'
  6260.  
  6261.     Overview
  6262.           Uses virtual memory hardware to protect dynamically allocated
  6263.           memory in order to detect illegal memory accesses.
  6264.  
  6265.    * Enforcer
  6266.     Author
  6267.           Michael Sinz (<Enforcer@sinz.org>)
  6268.  
  6269.     License
  6270.           Free Software
  6271.  
  6272.     Platforms
  6273.           AmigaOS
  6274.  
  6275.     Location
  6276.           `http://www.iam.com/amiga/enforcer.html'
  6277.  
  6278.     Overview
  6279.           Sets up MMU tables to watch for illegal accesses to memory,
  6280.           such as the low page and non-existent pages.
  6281.  
  6282.    * FDA (Free Debug Allocator)
  6283.     Author
  6284.           Thomas Helvey (<tomh@inxpress.net>)
  6285.  
  6286.     License
  6287.           GNU General Public License
  6288.  
  6289.     Platforms
  6290.           Linux, Windows
  6291.  
  6292.     Location
  6293.           `http://www.debian.org/Packages/unstable/devel/fda.html'
  6294.  
  6295.     Overview
  6296.           Provides routines that can be plugged in to replace
  6297.           `malloc()', `calloc()', `realloc()' and `free()'.
  6298.  
  6299.    * Fortify
  6300.     Author
  6301.           Simon Bullen (<sbullen@cybergraphic.com.au>)
  6302.  
  6303.     License
  6304.           Free Software
  6305.  
  6306.     Platforms
  6307.           AmigaOS
  6308.  
  6309.     Location
  6310.           `http://www.geocities.com/SiliconValley/Horizon/8596/fortify.html'
  6311.  
  6312.     Overview
  6313.           Provides a fortified shell for memory allocations, trapping
  6314.           memory leaks, writes beyond and before memory blocks and
  6315.           writes to freed memory.
  6316.  
  6317.    * GC (Garbage Collector)
  6318.     Author
  6319.           Hans-J. Boehm (<boehm@acm.org>)
  6320.  
  6321.     License
  6322.           Free Software
  6323.  
  6324.     Platforms
  6325.           Various UNIX, AmigaOS, MS-DOS, Windows, MacOS
  6326.  
  6327.     Location
  6328.           `http://www.hpl.hp.com/personal/Hans_Boehm/gc/'
  6329.  
  6330.     Overview
  6331.           A general-purpose, garbage-collecting storage allocator that
  6332.           is intended to be used as a plug-in replacement for
  6333.           `malloc()', but can also be used to detect memory leaks.
  6334.  
  6335.    * GlowCode
  6336.     Author
  6337.           Electric Software, Inc. (<info@glowcode.com>)
  6338.  
  6339.     License
  6340.           Commercial Software
  6341.  
  6342.     Platforms
  6343.           Windows
  6344.  
  6345.     Location
  6346.           `http://www.glowcode.com/'
  6347.  
  6348.     Overview
  6349.           Provides a profiler, call coverage tool and resource browser
  6350.           which can detail memory leaks.
  6351.  
  6352.    * Great Circle
  6353.     Author
  6354.           Geodesic Systems (<info@geodesic.com>)
  6355.  
  6356.     License
  6357.           Commercial Software
  6358.  
  6359.     Platforms
  6360.           Various UNIX, Windows
  6361.  
  6362.     Location
  6363.           `http://www.geodesic.com/'
  6364.  
  6365.     Overview
  6366.           Provides complete heap profiling, allowing programmers to see
  6367.           what parts of a program are using the most memory with
  6368.           symbolic stack tracing.
  6369.  
  6370.    * HeapAgent
  6371.     Author
  6372.           MicroQuill (<info@microquill.com>)
  6373.  
  6374.     License
  6375.           Commercial Software
  6376.  
  6377.     Platforms
  6378.           Windows
  6379.  
  6380.     Location
  6381.           `http://www.microquill.com/'
  6382.  
  6383.     Overview
  6384.           Instruments the heap to provide heap error detection without
  6385.           the need to recompile any source code.
  6386.  
  6387.    * Insure++
  6388.     Author
  6389.           ParaSoft (<info@parasoft.com>)
  6390.  
  6391.     License
  6392.           Commercial Software
  6393.  
  6394.     Platforms
  6395.           Various UNIX, Windows
  6396.  
  6397.     Location
  6398.           `http://www.parasoft.com/'
  6399.  
  6400.     Overview
  6401.           Uses Source Code Instrumentation and Runtime Pointer Tracking
  6402.           technologies to pinpoint memory corruption, memory leaks,
  6403.           operations on unrelated pointers and more.  The Inuse
  6404.           graphical memory usage display tool is also provided with this
  6405.           software.
  6406.  
  6407.    * JMalloc
  6408.     Author
  6409.           Jeff Dunlop
  6410.  
  6411.     License
  6412.           Free Software
  6413.  
  6414.     Platforms
  6415.           MS-DOS, Windows
  6416.  
  6417.     Location
  6418.           `http://www.snippets.org/'
  6419.  
  6420.     Overview
  6421.           Provides tracing and debugging for `malloc()' and `operator
  6422.           new'.
  6423.  
  6424.    * JProbe
  6425.     Author
  6426.           KL Group (<info@klgroup.com>)
  6427.  
  6428.     License
  6429.           Commercial Software
  6430.  
  6431.     Platforms
  6432.           Various UNIX, Windows
  6433.  
  6434.     Location
  6435.           `http://www.klgroup.com/'
  6436.  
  6437.     Overview
  6438.           Helps pinpoint memory leaks in Java applications by tracking
  6439.           which objects hold references to other objects, and allows
  6440.           visualisation of memory usage in real-time.
  6441.  
  6442.    * Leak
  6443.     Author
  6444.           Christopher Phillips (<pefv700@hermes.chpc.utexas.edu>)
  6445.  
  6446.     License
  6447.           Free Software
  6448.  
  6449.     Platforms
  6450.           Various UNIX
  6451.  
  6452.     Location
  6453.           `http://sources.isc.org/devel/memleak/leak.txt'
  6454.  
  6455.     Overview
  6456.           Logs all calls to `malloc()' and related functions to
  6457.           database files with the filename and line number, then
  6458.           attempts to validate reallocations and deallocations and
  6459.           detect memory leaks.
  6460.  
  6461.    * LeakTracer
  6462.     Author
  6463.           Erwin Andreasen (<erwin@andreasen.org>)
  6464.  
  6465.     License
  6466.           Free Software
  6467.  
  6468.     Platforms
  6469.           Various UNIX
  6470.  
  6471.     Location
  6472.           `http://www.andreasen.org/LeakTracer/'
  6473.  
  6474.     Overview
  6475.           Detects memory leaks in C++ programs by overriding `operator
  6476.           new' and `operator delete'.
  6477.  
  6478.    * Leaky
  6479.     Author
  6480.           Kipp Hickman (<kipp@netscape.com>)
  6481.  
  6482.     License
  6483.           Netscape Public License
  6484.  
  6485.     Platforms
  6486.           Linux
  6487.  
  6488.     Location
  6489.           `http://www.mozilla.org/unix/leaky.html'
  6490.  
  6491.     Overview
  6492.           A program which helps find memory leaks and helps debug
  6493.           reference count problems with xpcom objects.
  6494.  
  6495.    * Malloc Debug
  6496.     Author
  6497.           Brandon S. Allbery <allbery@ncoast.org>
  6498.  
  6499.     License
  6500.           Free Software
  6501.  
  6502.     Platforms
  6503.           Various UNIX
  6504.  
  6505.     Location
  6506.           `http://www.leo.org/pub/comp/usenet/comp.sources.misc/malloc-debug/'
  6507.  
  6508.     Overview
  6509.           A debugging malloc package with stack traceback capability.
  6510.  
  6511.    * Malloc Debug Library
  6512.     Author
  6513.           Rammi (<rammi@quincunx.escape.de>)
  6514.  
  6515.     License
  6516.           Free Software
  6517.  
  6518.     Platforms
  6519.           Various UNIX
  6520.  
  6521.     Location
  6522.           `http://www.escape.de/users/quincunx/rmdebug.html'
  6523.  
  6524.     Overview
  6525.           Implements wrappers for the normal heap handling functions.
  6526.  
  6527.    * MallocTrace
  6528.     Author
  6529.           Mark Brader (<msb@sq.sq.com>)
  6530.  
  6531.     License
  6532.           Free Software
  6533.  
  6534.     Platforms
  6535.           Various UNIX
  6536.  
  6537.     Location
  6538.           `ftp://ftp.uu.net/usenet/comp.sources.unix/volume18/malloc-trace.Z'
  6539.  
  6540.     Overview
  6541.           A malloc package with call stack tracebacks.
  6542.  
  6543.    * MCheck
  6544.     Author
  6545.           Ronald Veldema (<rveldema@cs.vu.nl>)
  6546.  
  6547.     License
  6548.           GNU General Public License
  6549.  
  6550.     Platforms
  6551.           Linux
  6552.  
  6553.     Location
  6554.           `http://www.cs.vu.nl/~rveldema/mcheck/mcheck.html'
  6555.  
  6556.     Overview
  6557.           A memory usage and malloc checker for C and C++.  Comes with
  6558.           a Java application for browsing the trace files produced.
  6559.  
  6560.    * MEM
  6561.     Author
  6562.           Walter Bright
  6563.  
  6564.     License
  6565.           Free Software
  6566.  
  6567.     Platforms
  6568.           MS-DOS
  6569.  
  6570.     Location
  6571.           `http://www.snippets.org/'
  6572.  
  6573.     Overview
  6574.           A set of functions for debugging pointer and memory
  6575.           allocation problems.
  6576.  
  6577.    * MemCheck
  6578.     Author
  6579.           Stratosware Corporation (<info@stratosware.com>)
  6580.  
  6581.     License
  6582.           Commercial Software
  6583.  
  6584.     Platforms
  6585.           Windows
  6586.  
  6587.     Location
  6588.           `http://www.stratosware.com/'
  6589.  
  6590.     Overview
  6591.           Detects various run-time errors related to operating system
  6592.           resources and provides information on memory leaks.
  6593.  
  6594.    * MemDebug
  6595.     Author
  6596.           Rene Schmit (<rene.schmit@crpht.lu>)
  6597.  
  6598.     License
  6599.           Free Software
  6600.  
  6601.     Platforms
  6602.           Various UNIX, MS-DOS, Windows, MacOS
  6603.  
  6604.     Location
  6605.           `ftp://ftp.crpht.lu/pub/sources/memdebug/'
  6606.  
  6607.     Overview
  6608.           Provides memory management error detection, memory usage
  6609.           error detection, memory usage profiling and error simulation.
  6610.  
  6611.    * MemLeak
  6612.     Author
  6613.           Keith Packard (<keithp@ncd.com>)
  6614.  
  6615.     License
  6616.           Free Software
  6617.  
  6618.     Platforms
  6619.           Various UNIX
  6620.  
  6621.     Location
  6622.           `ftp://ftp.x.org/pub/R6.4/xc/util/memleak/'
  6623.  
  6624.     Overview
  6625.           Replaces the C library allocation functions and provides
  6626.           extensive memory checking, locating lost memory, detecting
  6627.           free memory still in use and stores to free memory along with
  6628.           stack tracebacks.
  6629.  
  6630.    * Memory Advisor
  6631.     Author
  6632.           PLATINUM Technology (<info@platinum.com>)
  6633.  
  6634.     License
  6635.           Commercial Software
  6636.  
  6637.     Platforms
  6638.           Various UNIX
  6639.  
  6640.     Location
  6641.           `http://www.platinum.com/'
  6642.  
  6643.     Overview
  6644.           Disassembles an object module into system-independent
  6645.           assembler code, inserts error checking instructions, then
  6646.           re-assembles the code.  Can also replace existing malloc
  6647.           libraries in order to provide greater error checking.
  6648.           Formerly known as Sentinel.
  6649.  
  6650.    * Memory Sleuth
  6651.     Author
  6652.           TurboPower (<info@turbopower.com>)
  6653.  
  6654.     License
  6655.           Commercial Software
  6656.  
  6657.     Platforms
  6658.           Windows
  6659.  
  6660.     Location
  6661.           `http://www.turbopower.com/'
  6662.  
  6663.     Overview
  6664.           Quickly tracks down memory leaks and resource allocation
  6665.           errors with C++Builder and Delphi.
  6666.  
  6667.    * Memprof
  6668.     Author
  6669.           Owen Taylor (<otaylor@redhat.com>)
  6670.  
  6671.     License
  6672.           GNU General Public License
  6673.  
  6674.     Platforms
  6675.           Linux
  6676.  
  6677.     Location
  6678.           `http://people.redhat.com/otaylor/memprof/'
  6679.  
  6680.     Overview
  6681.           A tool for profiling memory usage and detecting memory leaks.
  6682.  
  6683.    * Memproof
  6684.     Author
  6685.           AutomatedQA (<info@totalqa.com>)
  6686.  
  6687.     License
  6688.           Free Software
  6689.  
  6690.     Platforms
  6691.           Windows
  6692.  
  6693.     Location
  6694.           `http://www.totalqa.com/'
  6695.  
  6696.     Overview
  6697.           A memory and resource leak debugger for Borland's family of
  6698.           Windows compilers.
  6699.  
  6700.    * MemWatch
  6701.     Author
  6702.           Johan Lindh (<johan@link-data.com>)
  6703.  
  6704.     License
  6705.           Free Software
  6706.  
  6707.     Platforms
  6708.           Various UNIX, Windows
  6709.  
  6710.     Location
  6711.           `http://www.link-data.com/'
  6712.  
  6713.     Overview
  6714.           A fault-tolerant memory leak and corruption detection tool.
  6715.  
  6716.    * MemWatch
  6717.     Author
  6718.           Doug Walker (<walker@unx.sas.com>)
  6719.  
  6720.     License
  6721.           Free Software
  6722.  
  6723.     Platforms
  6724.           AmigaOS
  6725.  
  6726.     Location
  6727.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6728.  
  6729.     Overview
  6730.           Provides replacement memory allocation routines for adding
  6731.           lots of memory debugging features that you link into your
  6732.           program.
  6733.  
  6734.    * MM (Shared Memory Library)
  6735.     Author
  6736.           Ralf S. Engelschall (<rse@engelschall.com>)
  6737.  
  6738.     License
  6739.           Free Software
  6740.  
  6741.     Platforms
  6742.           Various UNIX, Windows
  6743.  
  6744.     Location
  6745.           `http://www.engelschall.com/sw/mm/'
  6746.  
  6747.     Overview
  6748.           Simplifies the usage (and can help debug) the use of shared
  6749.           memory between related processes.
  6750.  
  6751.    * Mmalloc
  6752.     Author
  6753.           Mike Haertel (<mike@ai.mit.edu>) and Fred Fish
  6754.           (<fnf@cygnus.com>)
  6755.  
  6756.     License
  6757.           GNU General Public License
  6758.  
  6759.     Platforms
  6760.           Various UNIX
  6761.  
  6762.     Location
  6763.           `http://www.gnu.org/'
  6764.  
  6765.     Overview
  6766.           Uses `mmap()' to allocate separate pools of memory which can
  6767.           be mapped onto files for later reuse.
  6768.  
  6769.    * MPR
  6770.     Author
  6771.           Taj Khattra (<taj.khattra@pobox.com>)
  6772.  
  6773.     License
  6774.           Free Software
  6775.  
  6776.     Platforms
  6777.           Linux
  6778.  
  6779.     Location
  6780.           `http://metalab.unc.edu/pub/Linux/devel/lang/c/mpr-2.0.tar.gz'
  6781.  
  6782.     Overview
  6783.           Attempts to find memory leaks in C/C++ programs by writing a
  6784.           log file during program execution, which can then be
  6785.           processed for obtaining further information.
  6786.  
  6787.    * Mprof
  6788.     Author
  6789.           Ben Zorn (<zorn@cs.colorado.edu>)
  6790.  
  6791.     License
  6792.           Free Software
  6793.  
  6794.     Platforms
  6795.           Various UNIX
  6796.  
  6797.     Location
  6798.           `ftp://gatekeeper.dec.com/pub/misc/mprof-3.0.tar.Z'
  6799.  
  6800.     Overview
  6801.           Profiles the dynamic memory allocation behaviour of programs
  6802.           by logging details for each function than makes a memory
  6803.           allocation, including call stack tracebacks.
  6804.  
  6805.    * MuForce
  6806.     Author
  6807.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6808.  
  6809.     License
  6810.           Free Software
  6811.  
  6812.     Platforms
  6813.           AmigaOS
  6814.  
  6815.     Location
  6816.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6817.  
  6818.     Overview
  6819.           Uses the MMU to monitor the system for any writes to
  6820.           non-existent memory and reports them over the serial port or
  6821.           any other output stream.
  6822.  
  6823.    * MuGuardianAngel
  6824.     Author
  6825.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6826.  
  6827.     License
  6828.           Free Software
  6829.  
  6830.     Platforms
  6831.           AmigaOS
  6832.  
  6833.     Location
  6834.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6835.  
  6836.     Overview
  6837.           An extension to the MuForce program which protects free
  6838.           memory and detects all illegal memory accesses.
  6839.  
  6840.    * MuLib
  6841.     Author
  6842.           Thomas Richter (<thor@einstein.math.tu-berlin.de>)
  6843.  
  6844.     License
  6845.           Free Software
  6846.  
  6847.     Platforms
  6848.           AmigaOS
  6849.  
  6850.     Location
  6851.           `http://www.math.tu-berlin.de/~thor/thor/index.html'
  6852.  
  6853.     Overview
  6854.           Provides access to the MMU in modern Amigas so that features
  6855.           such as virtual memory can be implemented.
  6856.  
  6857.    * Mungwall
  6858.     Author
  6859.           Commodore-Amiga, Inc. (<info@amiga.de>)
  6860.  
  6861.     License
  6862.           Free Software
  6863.  
  6864.     Platforms
  6865.           AmigaOS
  6866.  
  6867.     Location
  6868.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  6869.  
  6870.     Overview
  6871.           Patches the system to check for free memory corruption.
  6872.  
  6873.    * NJAMD (Not Just Another Malloc Debugger)
  6874.     Author
  6875.           Mike Perry (<mikepery@fscked.org>)
  6876.  
  6877.     License
  6878.           GNU General Public License
  6879.  
  6880.     Platforms
  6881.           Various UNIX
  6882.  
  6883.     Location
  6884.           `http://fscked.org/proj/njamd.shtml/'
  6885.  
  6886.     Overview
  6887.           Helps track down a wide range of memory allocation problems
  6888.           and is divided into a front end executable and a library back
  6889.           end.
  6890.  
  6891.    * Optimizeit
  6892.     Author
  6893.           Intuitive Systems, Inc. (<info@optimizeit.com>)
  6894.  
  6895.     License
  6896.           Commercial Software
  6897.  
  6898.     Platforms
  6899.           Various UNIX, Windows
  6900.  
  6901.     Location
  6902.           `http://www.optimizeit.com/'
  6903.  
  6904.     Overview
  6905.           Attempts to locate memory leaks and performance bottlenecks
  6906.           in Java programs.
  6907.  
  6908.    * Plumber
  6909.     Author
  6910.           Owen O'Malley (<omalley@ics.uci.edu>)
  6911.  
  6912.     License
  6913.           GNU General Public License
  6914.  
  6915.     Platforms
  6916.           Linux, Solaris, SunOS
  6917.  
  6918.     Location
  6919.           `http://www.ics.uci.edu/~softtest/plumber.html'
  6920.  
  6921.     Overview
  6922.           A tool that replaces the normal Ada and C/C++ dynamic memory
  6923.           allocation functions and detects unfreed memory blocks.
  6924.  
  6925.    * Purify
  6926.     Author
  6927.           Rational Software (<info@rational.com>)
  6928.  
  6929.     License
  6930.           Commercial Software
  6931.  
  6932.     Platforms
  6933.           Various UNIX, Windows
  6934.  
  6935.     Location
  6936.           `http://www.rational.com/'
  6937.  
  6938.     Overview
  6939.           Uses Object Code Insertion technology to provide run-time
  6940.           error checking and memory leak detection.
  6941.  
  6942.    * QC
  6943.     Author
  6944.           Onyx Technology (<sales@onyx-tech.com>)
  6945.  
  6946.     License
  6947.           Commercial Software
  6948.  
  6949.     Platforms
  6950.           MacOS
  6951.  
  6952.     Location
  6953.           `http://www.onyx-tech.com/'
  6954.  
  6955.     Overview
  6956.           Runs in the background as a control panel and detects various
  6957.           memory errors which can then be caught and run under a
  6958.           debugger.
  6959.  
  6960.    * TestCenter
  6961.     Author
  6962.           CenterLine Development Systems (<info@centerline.com>)
  6963.  
  6964.     License
  6965.           Commercial Software
  6966.  
  6967.     Platforms
  6968.           Various UNIX
  6969.  
  6970.     Location
  6971.           `http://www.centerline.com/'
  6972.  
  6973.     Overview
  6974.           Detects memory leaks, duplicate frees and illegal access
  6975.           errors including loads from uninitialised objects.
  6976.  
  6977.    * Third Degree
  6978.     Author
  6979.           Digital Equipment Corporation (<info@digital.com>)
  6980.  
  6981.     License
  6982.           Commercial Software
  6983.  
  6984.     Platforms
  6985.           Digital UNIX
  6986.  
  6987.     Location
  6988.           `http://www.digital.com/'
  6989.  
  6990.     Overview
  6991.           A tool that performs memory access checks and memory leak
  6992.           detection of C, C++ and Fortran programs at run-time.
  6993.           Applications are modified using ATOM to determine if any
  6994.           memory locations are accessed when not properly allocated or
  6995.           initialised.
  6996.  
  6997.    * Vmalloc
  6998.     Author
  6999.           Kiem-Phong Vo (<kpv@research.att.com>)
  7000.  
  7001.     License
  7002.           AT&T Source Code License
  7003.  
  7004.     Platforms
  7005.           Various UNIX, Windows
  7006.  
  7007.     Location
  7008.           `http://akpublic.research.att.com/sw/tools/vmalloc/'
  7009.  
  7010.     Overview
  7011.           A discipline and method library for dynamic memory
  7012.           allocation, with support for regions, debugging and profiling.
  7013.  
  7014.    * Wipeout
  7015.     Author
  7016.           Olaf Barthel (<olsen@sourcery.han.de>)
  7017.  
  7018.     License
  7019.           Free Software
  7020.  
  7021.     Platforms
  7022.           AmigaOS
  7023.  
  7024.     Location
  7025.           `http://wuarchive.wustl.edu/~aminet/dirs/dev_debug.html'
  7026.  
  7027.     Overview
  7028.           Runs in the background checking free memory for corruption.
  7029.  
  7030.    * YAMD (Yet Another Malloc Debugger)
  7031.     Author
  7032.           Nate Eldredge (<neldredge@hmc.edu>)
  7033.  
  7034.     License
  7035.           GNU General Public License
  7036.  
  7037.     Platforms
  7038.           Linux, DOS
  7039.  
  7040.     Location
  7041.           `http://www3.hmc.edu/~neldredge/yamd/'
  7042.  
  7043.     Overview
  7044.           A tool for finding bugs related to dynamic memory allocation
  7045.           in C and C++, and includes paging mechanisms to catch bugs
  7046.           immediately.
  7047.  
  7048.    * ZeroFault
  7049.     Author
  7050.           The Kernel Group (<info@zerofault.com>)
  7051.  
  7052.     License
  7053.           Commercial Software
  7054.  
  7055.     Platforms
  7056.           AIX UNIX
  7057.  
  7058.     Location
  7059.           `http://www.zerofault.com/'
  7060.  
  7061.     Overview
  7062.           Uses run-time emulator technology to provide run-time error
  7063.           checking and memory leak detection.
  7064.  
  7065.    However, before you try out any of the above software, there may
  7066. already be a malloc library with debugging support on your system that
  7067. might be suitable for solving your problem.  For example, on Solaris 7
  7068. the following libraries are available:
  7069.  
  7070. `malloc(3c)'
  7071.      Trade-off between performance and efficiency.
  7072.  
  7073. `malloc(3x)'
  7074.      Slower performance, space-efficient.
  7075.  
  7076. `bsdmalloc(3x)'
  7077.      Better performance, space-inefficient.
  7078.  
  7079. `mtmalloc(3t)'
  7080.      Thread-safe memory allocator.
  7081.  
  7082. `mapmalloc(3x)'
  7083.      Uses `mmap()' instead of `sbrk()' to allocate heap space.
  7084.  
  7085. `watchmalloc(3x)'
  7086.      Uses watch point areas to check for overflows.
  7087.  
  7088.    On platforms with the GNU C library, such as Linux, there are several
  7089. environment variables that can be used to enable various debugging
  7090. features of `malloc()', etc.  There are also extra functions provided
  7091. in the library which can be used to aid in debugging, and some shell
  7092. scripts which can translate return addresses or locate unfreed memory
  7093. allocations in the log files produced.  Useful information on the
  7094. debugging features available within the GNU C library is located at
  7095. `http://sdb.suse.de/sdb/en/html/aj_debug.html'.
  7096.  
  7097.    ---------- Footnotes ----------
  7098.  
  7099.    (1) This list can be considered to be a slightly more up to date
  7100. version of `Debugging Tools for Dynamic Storage Allocation and Memory
  7101. Management' (`http://www.cs.colorado.edu/~zorn/MallocDebug.html') by Ben
  7102. Zorn (<zorn@cs.colorado.edu>).
  7103.  
  7104. 
  7105. File: mpatrol.info,  Node: Function index,  Next: Index,  Prev: Related software,  Up: Top
  7106.  
  7107. Function index
  7108. **************
  7109.  
  7110. * Menu:
  7111.  
  7112. * __mp_check:                            Functions.
  7113. * __mp_epilogue:                         Functions.
  7114. * __mp_info:                             Functions.
  7115. * __mp_memorymap:                        Functions.
  7116. * __mp_nomemory:                         Functions.
  7117. * __mp_printinfo:                        Functions.
  7118. * __mp_prologue:                         Functions.
  7119. * __mp_summary:                          Functions.
  7120. * bcmp:                                  Functions.
  7121. * bcopy:                                 Functions.
  7122. * bzero:                                 Functions.
  7123. * calloc:                                Functions.
  7124. * cfree:                                 Functions.
  7125. * expand:                                Functions.
  7126. * free:                                  Functions.
  7127. * malloc:                                Functions.
  7128. * memalign:                              Functions.
  7129. * memccpy:                               Functions.
  7130. * memchr:                                Functions.
  7131. * memcmp:                                Functions.
  7132. * memcpy:                                Functions.
  7133. * memmem:                                Functions.
  7134. * memmove:                               Functions.
  7135. * memset:                                Functions.
  7136. * operator delete:                       Functions.
  7137. * operator delete[]:                     Functions.
  7138. * operator new:                          Functions.
  7139. * operator new[]:                        Functions.
  7140. * pvalloc:                               Functions.
  7141. * realloc:                               Functions.
  7142. * recalloc:                              Functions.
  7143. * set_new_handler:                       Functions.
  7144. * strdup:                                Functions.
  7145. * strndup:                               Functions.
  7146. * strnsave:                              Functions.
  7147. * strsave:                               Functions.
  7148. * valloc:                                Functions.
  7149.  
  7150. 
  7151. File: mpatrol.info,  Node: Index,  Prev: Function index,  Up: Top
  7152.  
  7153. Index
  7154. *****
  7155.  
  7156. * Menu:
  7157.  
  7158. * -1:                                    Options.
  7159. * -2:                                    Options.
  7160. * -3:                                    Options.
  7161. * -A:                                    Options.
  7162. * -a:                                    Options.
  7163. * -C:                                    Options.
  7164. * -c:                                    Options.
  7165. * -d:                                    Options.
  7166. * -D:                                    Options.
  7167. * -e:                                    Options.
  7168. * -F:                                    Options.
  7169. * -f:                                    Options.
  7170. * -g:                                    Options.
  7171. * -G:                                    Options.
  7172. * -L:                                    Options.
  7173. * -l:                                    Options.
  7174. * -M:                                    Options.
  7175. * -m:                                    Options.
  7176. * -N:                                    Options.
  7177. * -n:                                    Options.
  7178. * -O:                                    Options.
  7179. * -o:                                    Options.
  7180. * -p:                                    Options.
  7181. * -P:                                    Options.
  7182. * -Q:                                    Options.
  7183. * -R:                                    Options.
  7184. * -S:                                    Options.
  7185. * -s:                                    Options.
  7186. * -U:                                    Options.
  7187. * -V:                                    Options.
  7188. * -v:                                    Options.
  7189. * -w:                                    Options.
  7190. * -x:                                    Options.
  7191. * -X:                                    Options.
  7192. * -Z:                                    Options.
  7193. * -z:                                    Options.
  7194. * .gdbinit:                              Using with a debugger.
  7195. * _RLD_LIST:                             Utilities.
  7196. * ABI:                                   Call stacks and symbol tables.
  7197. * acknowledgements:                      Foreword.
  7198. * adding a new object file format:       New file format support.
  7199. * adding a new operating system:         New system support.
  7200. * adding a new processor architecture:   New processor support.
  7201. * address space:                         Operating system support.
  7202. * address, physical:                     Virtual memory.
  7203. * address, virtual:                      Virtual memory.
  7204. * AIX, IBM RS/6000:                      Supported systems.
  7205. * alignment:                             Library behaviour.
  7206. * all (make target):                     Installation.
  7207. * alloca:                                Dynamic memory allocations.
  7208. * allocated blocks:                      Example 1.
  7209. * allocation algorithm:                  How it works.
  7210. * allocation bin table:                  Profiling.
  7211. * allocation bins:                       Profiling.
  7212. * allocation boundaries:                 Profiling.
  7213. * allocation byte:                       General errors.
  7214. * allocation index:                      Example 1.
  7215. * allocation information:                Functions.
  7216. * allocation type:                       Example 1.
  7217. * ALLOCBYTE:                             Environment.
  7218. * ALLOCSTOP:                             Environment.
  7219. * ALLOWOFLOW:                            Environment.
  7220. * amalloc:                               Improving performance.
  7221. * Amiga 4000/040:                        Installation.
  7222. * Amiga notes:                           Amiga notes.
  7223. * AmigaOS, Motorola 680x0:               Supported systems.
  7224. * ANSI:                                  Improving performance.
  7225. * application binary interface:          Call stacks and symbol tables.
  7226. * APurify:                               Related software.
  7227. * AR:                                    Installation.
  7228. * archive library:                       Features.
  7229. * arenas:                                Improving performance.
  7230. * ATOM:                                  Related software.
  7231. * author, contacting:                    Foreword.
  7232. * AutomatedQA:                           Related software.
  7233. * AUTOSAVE:                              Environment.
  7234. * BASIC:                                 Memory allocations.
  7235. * batch testing:                         Testing.
  7236. * best fit:                              How it works.
  7237. * BFD:                                   Examples.
  7238. * bin:                                   Profiling.
  7239. * binary:                                Environment.
  7240. * binary file:                           Profiling.
  7241. * blocks:                                Example 1.
  7242. * BoundsChecker:                         Related software.
  7243. * breakpoint:                            Using with a debugger.
  7244. * bsdmalloc(3x):                         Related software.
  7245. * BSS:                                   Static memory allocations.
  7246. * buffers, overflow:                     Overwrites and underwrites.
  7247. * bug reports:                           Foreword.
  7248. * bugs:                                  Notes.
  7249. * building the library:                  Installation.
  7250. * bus errors:                            Library behaviour.
  7251. * bytes compared:                        Example 1.
  7252. * bytes copied:                          Example 1.
  7253. * bytes located:                         Example 1.
  7254. * bytes set:                             Example 1.
  7255. * C:                                     Memory allocations.
  7256. * C++:                                   Memory allocations.
  7257. * C++ mangled names:                     Example 1.
  7258. * call sites:                            Profiling.
  7259. * call stacks:                           Call stacks and symbol tables.
  7260. * call-by-value:                         Stack memory allocations.
  7261. * callback functions:                    Example 1.
  7262. * calling convention:                    Call stacks and symbol tables.
  7263. * CC:                                    Installation.
  7264. * Ccmalloc:                              Related software.
  7265. * CenterLine Development Systems:        Related software.
  7266. * CFLAGS:                                Installation.
  7267. * Chaperon:                              Related software.
  7268. * CHECK:                                 Environment.
  7269. * CHECKALL:                              Environment.
  7270. * CHECKALLOCS:                           Environment.
  7271. * Checker:                               Related software.
  7272. * CHECKFREES:                            Environment.
  7273. * CHECKREALLOCS:                         Environment.
  7274. * clean (make target):                   Installation.
  7275. * clobber (make target):                 Installation.
  7276. * COFF:                                  Examples.
  7277. * command line options:                  Options.
  7278. * Commodore-Amiga, Inc.:                 Related software.
  7279. * common variables:                      Static memory allocations.
  7280. * compiler:                              Installation.
  7281. * compiling:                             Installation.
  7282. * contacting the author:                 Foreword.
  7283. * contributors:                          Foreword.
  7284. * crash:                                 Example 1.
  7285. * CSRI malloc:                           Related software.
  7286. * data sections:                         Static memory allocations.
  7287. * Dbmalloc:                              Related software.
  7288. * Debauch:                               Related software.
  7289. * Debug Heap:                            Related software.
  7290. * debugger:                              Using with a debugger.
  7291. * debugging:                             Using with a debugger.
  7292. * debugging information:                 Call stacks and symbol tables.
  7293. * decimal:                               Environment.
  7294. * declarations, tentative:               Static memory allocations.
  7295. * DEFALIGN:                              Environment.
  7296. * demangler:                             Example 1.
  7297. * DG/UX, Intel 80x86:                    Supported systems.
  7298. * DG/UX, Motorola 88xx0:                 Supported systems.
  7299. * Digital Equipment Corporation:         Related software.
  7300. * Digital UNIX:                          Improving performance.
  7301. * direct allocation table:               Profiling.
  7302. * direct allocations:                    Profiling.
  7303. * DLLs:                                  Call stacks and symbol tables.
  7304. * Dmalloc:                               Related software.
  7305. * documentation:                         Installation.
  7306. * dumping memory:                        Example 4.
  7307. * dynamic link libraries:                Call stacks and symbol tables.
  7308. * dynamic linker:                        Call stacks and symbol tables.
  7309. * dynamic linking:                       Call stacks and symbol tables.
  7310. * dynamic memory allocations:            Dynamic memory allocations.
  7311. * DYNIX/ptx, Intel 80x86:                Supported systems.
  7312. * Electric Fence:                        Related software.
  7313. * Electric Software, Inc.:               Related software.
  7314. * ELF32:                                 Examples.
  7315. * embedded libraries:                    Utilities.
  7316. * embedded systems:                      Operating system support.
  7317. * Enforcer:                              Related software.
  7318. * enhancements:                          Notes.
  7319. * entry-point:                           Example 1.
  7320. * environment:                           Environment.
  7321. * epilogue function:                     Example 1.
  7322. * error severity:                        Example 1.
  7323. * errors, run-time:                      Overview.
  7324. * examples:                              Examples.
  7325. * executable files:                      Call stacks and symbol tables.
  7326. * FAILFREQ:                              Environment.
  7327. * FAILSEED:                              Environment.
  7328. * failure frequency:                     Testing.
  7329. * failure seed:                          Testing.
  7330. * FAQ:                                   Foreword.
  7331. * fatal errors:                          Example 1.
  7332. * fault, page:                           Virtual memory.
  7333. * FDA (Free Debug Allocator):            Related software.
  7334. * features:                              Features.
  7335. * fence posts:                           Overwrites and underwrites.
  7336. * file scope variables:                  Static memory allocations.
  7337. * files, mapping:                        Virtual memory.
  7338. * first fit:                             How it works.
  7339. * fitting allocations:                   Example 3.
  7340. * foreword:                              Foreword.
  7341. * Fortify:                               Related software.
  7342. * FORTRAN:                               Memory allocations.
  7343. * free blocks:                           Example 1.
  7344. * free byte:                             General errors.
  7345. * free memory:                           General errors.
  7346. * FREEBYTE:                              Environment.
  7347. * freed blocks:                          Example 1.
  7348. * freed memory:                          General errors.
  7349. * FREESTOP:                              Environment.
  7350. * FreshMeat:                             Foreword.
  7351. * function call stacks:                  Call stacks and symbol tables.
  7352. * functions:                             Functions.
  7353. * functions, callback:                   Example 1.
  7354. * functions, handler:                    Example 1.
  7355. * future enhancements:                   Notes.
  7356. * g++:                                   Example 1.
  7357. * garbage collector:                     Dynamic memory allocations.
  7358. * GC (Garbage Collector):                Related software.
  7359. * gcc:                                   Example 1.
  7360. * gdb:                                   Using with a debugger.
  7361. * general errors:                        General errors.
  7362. * Geodesic Systems:                      Related software.
  7363. * getting updates:                       Foreword.
  7364. * GlowCode:                              Related software.
  7365. * GNU C library:                         Related software.
  7366. * Great Circle:                          Related software.
  7367. * halting the library:                   Using with a debugger.
  7368. * handler functions:                     Example 1.
  7369. * heap:                                  Dynamic memory allocations.
  7370. * heap usage:                            Example 1.
  7371. * HeapAgent:                             Related software.
  7372. * HELP:                                  Environment.
  7373. * hexadecimal:                           Environment.
  7374. * hidden memory:                         How it works.
  7375. * hints:                                 Improving performance.
  7376. * HP/UX, HP PA/RISC:                     Supported systems.
  7377. * IBM Corporation:                       Related software.
  7378. * illegal memory accesses:               Example 3.
  7379. * implementation details:                How it works.
  7380. * improving performance:                 Improving performance.
  7381. * information about an allocation:       Functions.
  7382. * installation:                          Installation.
  7383. * Insure++:                              Related software.
  7384. * integration:                           Integration.
  7385. * internal blocks:                       Example 1.
  7386. * Intuitive Systems, Inc.:               Related software.
  7387. * Inuse:                                 Installation.
  7388. * IRIX, MIPS:                            Supported systems.
  7389. * JMalloc:                               Related software.
  7390. * JProbe:                                Related software.
  7391. * Kernel Group, The:                     Related software.
  7392. * KL Group:                              Related software.
  7393. * known bugs:                            Notes.
  7394. * LARGEBOUND:                            Environment.
  7395. * LaTeX:                                 Installation.
  7396. * LD:                                    Installation.
  7397. * LD_PRELOAD:                            Utilities.
  7398. * Leak:                                  Related software.
  7399. * LeakTracer:                            Related software.
  7400. * Leaky:                                 Related software.
  7401. * library behaviour:                     Library behaviour.
  7402. * library functions:                     Library functions.
  7403. * library settings:                      Logging and tracing.
  7404. * library statistics:                    Logging and tracing.
  7405. * library, archive:                      Features.
  7406. * library, building:                     Installation.
  7407. * library, mpatrol:                      Overview.
  7408. * library, shared:                       Features.
  7409. * library, thread-safe:                  Features.
  7410. * LIMIT:                                 Environment.
  7411. * limitations:                           Notes.
  7412. * limiting available memory:             Testing.
  7413. * line number table:                     Call stacks and symbol tables.
  7414. * linker:                                Installation.
  7415. * linking:                               Installation.
  7416. * links, symbolic:                       Installation.
  7417. * lint:                                  Installation.
  7418. * lint (make target):                    Installation.
  7419. * Linux, Intel 80x86:                    Supported systems.
  7420. * Linux, Motorola 680x0:                 Supported systems.
  7421. * local static variables:                Static memory allocations.
  7422. * log file:                              Example 1.
  7423. * LOGALL:                                Environment.
  7424. * LOGALLOCS:                             Environment.
  7425. * LOGFILE:                               Environment.
  7426. * LOGFREES:                              Environment.
  7427. * logging:                               Logging and tracing.
  7428. * LOGMEMORY:                             Environment.
  7429. * LOGREALLOCS:                           Environment.
  7430. * low memory handler function:           Example 1.
  7431. * LynxOS, PowerPC:                       Supported systems.
  7432. * make:                                  Installation.
  7433. * Makefile:                              Installation.
  7434. * Malloc Debug:                          Related software.
  7435. * Malloc Debug Library:                  Related software.
  7436. * malloc libraries for Solaris 7:        Related software.
  7437. * malloc(3c):                            Related software.
  7438. * malloc(3x):                            Related software.
  7439. * Malloc_Dbg:                            Related software.
  7440. * MallocTrace:                           Related software.
  7441. * mangled names:                         Example 1.
  7442. * manual layout:                         Foreword.
  7443. * manual pages:                          Installation.
  7444. * map of memory:                         Logging and tracing.
  7445. * mapmalloc(3x):                         Related software.
  7446. * mapping files:                         Virtual memory.
  7447. * MCheck:                                Related software.
  7448. * MEDIUMBOUND:                           Environment.
  7449. * MEM:                                   Related software.
  7450. * MemCheck:                              Related software.
  7451. * MemDebug:                              Related software.
  7452. * MemLeak:                               Related software.
  7453. * Memory Advisor:                        Related software.
  7454. * memory allocation profiling:           Profiling.
  7455. * memory allocations:                    Memory allocations.
  7456. * memory allocations, dynamic:           Dynamic memory allocations.
  7457. * memory allocations, stack:             Stack memory allocations.
  7458. * memory allocations, static:            Static memory allocations.
  7459. * memory blocks:                         Example 1.
  7460. * memory dump:                           Example 4.
  7461. * memory leak table:                     Profiling.
  7462. * memory leaks:                          Example 7.
  7463. * memory management interface:           Operating system support.
  7464. * memory management unit:                Virtual memory.
  7465. * memory map:                            Logging and tracing.
  7466. * memory mapped files:                   Virtual memory.
  7467. * memory protection:                     Virtual memory.
  7468. * Memory Sleuth:                         Related software.
  7469. * memory usage:                          Example 1.
  7470. * memory, physical:                      Virtual memory.
  7471. * memory, virtual:                       Virtual memory.
  7472. * Memprof:                               Related software.
  7473. * Memproof:                              Related software.
  7474. * MemWatch:                              Related software.
  7475. * message passing:                       Threads.
  7476. * MicroQuill:                            Related software.
  7477. * Microsoft:                             Supported systems.
  7478. * misaligned data:                       Library behaviour.
  7479. * misaligned memory accesses:            Virtual memory.
  7480. * ML:                                    Dynamic memory allocations.
  7481. * mleak:                                 Utilities.
  7482. * MM (Shared Memory Library):            Related software.
  7483. * Mmalloc:                               Related software.
  7484. * mmap:                                  Library behaviour.
  7485. * MMU:                                   Virtual memory.
  7486. * modules:                               How it works.
  7487. * MP_NOCPLUSPLUS:                        Functions.
  7488. * mpatrol:                               Overview.
  7489. * mpatrol command:                       Options.
  7490. * mpatrol features:                      Features.
  7491. * mpatrol library:                       Overview.
  7492. * mpatrol.h:                             Functions.
  7493. * mpatrol.log:                           Example 1.
  7494. * MPATROL_OPTIONS:                       Environment.
  7495. * MPATROL_VERSION:                       Functions.
  7496. * MPR:                                   Related software.
  7497. * Mprof:                                 Related software.
  7498. * mprof:                                 Profiling.
  7499. * mtmalloc(3t):                          Related software.
  7500. * MuForce:                               Related software.
  7501. * MuGuardianAngel:                       Related software.
  7502. * MuLib:                                 Related software.
  7503. * multi-processor systems:               Threads.
  7504. * Mungwall:                              Related software.
  7505. * mutexes:                               Threads.
  7506. * NDEBUG:                                Functions.
  7507. * Netware notes:                         Netware notes.
  7508. * NJAMD (Not Just Another Malloc Debugger): Related software.
  7509. * NOFREE:                                Environment.
  7510. * non-static local variables:            Stack memory allocations.
  7511. * NOPROTECT:                             Environment.
  7512. * notes:                                 Notes.
  7513. * notes for all platforms:               Generic notes.
  7514. * notes for Amiga platforms:             Amiga notes.
  7515. * notes for Netware platforms:           Netware notes.
  7516. * notes for UNIX platforms:              UNIX notes.
  7517. * notes for Windows platforms:           Windows notes.
  7518. * NuMega Corporation:                    Related software.
  7519. * object file formats, adding support:   New file format support.
  7520. * object files:                          Call stacks and symbol tables.
  7521. * octal:                                 Environment.
  7522. * OFLAGS:                                Installation.
  7523. * OFLOWBYTE:                             Environment.
  7524. * OFLOWSIZE:                             Environment.
  7525. * OFLOWWATCH:                            Environment.
  7526. * Onyx Technology:                       Related software.
  7527. * operating systems:                     Operating system support.
  7528. * operating systems, adding support:     New system support.
  7529. * optimisation:                          Installation.
  7530. * Optimizeit:                            Related software.
  7531. * option summary:                        Environment.
  7532. * options:                               Options.
  7533. * original implementation:               How it works.
  7534. * other programs:                        Related software.
  7535. * overflow buffers:                      Overwrites and underwrites.
  7536. * overflow byte:                         Overwrites and underwrites.
  7537. * overflow size:                         Overwrites and underwrites.
  7538. * overview:                              Overview.
  7539. * overwrites:                            Overwrites and underwrites.
  7540. * page:                                  Virtual memory.
  7541. * page fault:                            Virtual memory.
  7542. * page size:                             Virtual memory.
  7543. * PAGEALLOC:                             Environment.
  7544. * parallel programming:                  Threads.
  7545. * parameter variables:                   Stack memory allocations.
  7546. * Parasoft:                              Related software.
  7547. * Pascal:                                Memory allocations.
  7548. * peak memory usage:                     Example 1.
  7549. * performance bottleneck:                Improving performance.
  7550. * performance improvements:              Improving performance.
  7551. * performance times:                     Library performance.
  7552. * physical address:                      Virtual memory.
  7553. * physical memory:                       Virtual memory.
  7554. * platform-independent notes:            Generic notes.
  7555. * platforms:                             Supported systems.
  7556. * PLATINUM Technology:                   Related software.
  7557. * Plumber:                               Related software.
  7558. * portability:                           Improving performance.
  7559. * POSIX threads:                         Threads.
  7560. * PRESERVE:                              Environment.
  7561. * preserve freed contents:               General errors.
  7562. * prevent freeing memory:                General errors.
  7563. * printing:                              Installation.
  7564. * process id:                            Options.
  7565. * processor architectures, adding support: New processor support.
  7566. * PROF:                                  Environment.
  7567. * PROFFILE:                              Environment.
  7568. * profiling:                             Profiling.
  7569. * profiling file format:                 Profiling file format.
  7570. * PROGFILE:                              Environment.
  7571. * program counter:                       Call stacks and symbol tables.
  7572. * programs:                              Related software.
  7573. * prologue function:                     Example 1.
  7574. * Purify:                                Related software.
  7575. * QC:                                    Related software.
  7576. * quick reference card:                  Installation.
  7577. * random failures:                       Testing.
  7578. * Rational Software:                     Related software.
  7579. * re-entrancy:                           Threads.
  7580. * read protection:                       Virtual memory.
  7581. * REALLOCSTOP:                           Environment.
  7582. * recompilation:                         Integration.
  7583. * recoverable errors:                    Example 1.
  7584. * RedHat:                                Supported systems.
  7585. * reference card:                        Installation.
  7586. * references:                            Foreword.
  7587. * registers:                             Stack memory allocations.
  7588. * related software:                      Related software.
  7589. * release builds:                        Foreword.
  7590. * reporting bugs:                        Foreword.
  7591. * return address:                        Call stacks and symbol tables.
  7592. * run-time errors:                       Overview.
  7593. * SAFESIGNALS:                           Environment.
  7594. * sbrk:                                  Library behaviour.
  7595. * sections:                              Static memory allocations.
  7596. * semaphores:                            Threads.
  7597. * Sentinel:                              Related software.
  7598. * settings:                              Logging and tracing.
  7599. * severity of errors:                    Example 1.
  7600. * SFLAGS:                                Installation.
  7601. * shared libraries:                      Call stacks and symbol tables.
  7602. * shared library:                        Features.
  7603. * shared memory:                         Threads.
  7604. * SHOWALL:                               Environment.
  7605. * SHOWFREED:                             Environment.
  7606. * SHOWMAP:                               Environment.
  7607. * SHOWSYMBOLS:                           Environment.
  7608. * SHOWUNFREED:                           Environment.
  7609. * signal handler:                        Example 3.
  7610. * signals:                               Features.
  7611. * similar programs:                      Related software.
  7612. * single-step:                           Using with a debugger.
  7613. * slot tables:                           Improving performance.
  7614. * SMALLBOUND:                            Environment.
  7615. * software:                              Related software.
  7616. * Solaris 7 malloc libraries:            Related software.
  7617. * Solaris, Intel 80x86:                  Supported systems.
  7618. * Solaris, SPARC:                        Supported systems.
  7619. * stack:                                 Stack memory allocations.
  7620. * stack memory allocations:              Stack memory allocations.
  7621. * stack tracebacks:                      Call stacks and symbol tables.
  7622. * static memory allocations:             Static memory allocations.
  7623. * statistics:                            Logging and tracing.
  7624. * Stratosware Corporation:               Related software.
  7625. * stress testing:                        Improving performance.
  7626. * stripped executable file:              Library behaviour.
  7627. * summary of options:                    Environment.
  7628. * supported systems:                     Supported systems.
  7629. * SVR4:                                  Examples.
  7630. * swap file:                             Virtual memory.
  7631. * swap in:                               Virtual memory.
  7632. * swap out:                              Virtual memory.
  7633. * swapping:                              Virtual memory.
  7634. * symbol summary:                        Logging and tracing.
  7635. * symbol tables:                         Call stacks and symbol tables.
  7636. * symbolic links:                        Installation.
  7637. * symbols:                               Call stacks and symbol tables.
  7638. * system page size:                      Virtual memory.
  7639. * systems:                               Supported systems.
  7640. * systems, embedded:                     Operating system support.
  7641. * tentative declarations:                Static memory allocations.
  7642. * test suite:                            Features.
  7643. * TestCenter:                            Related software.
  7644. * testing:                               Testing.
  7645. * TeXinfo:                               Installation.
  7646. * TFLAGS:                                Installation.
  7647. * Third Degree:                          Related software.
  7648. * thrashing:                             Virtual memory.
  7649. * thread-safe library:                   Features.
  7650. * threads:                               Threads.
  7651. * threads library:                       Threads.
  7652. * times:                                 Library performance.
  7653. * tips:                                  Improving performance.
  7654. * tracebacks:                            Call stacks and symbol tables.
  7655. * tracing:                               Logging and tracing.
  7656. * tree structure:                        Example 2.
  7657. * TurboPower:                            Related software.
  7658. * tutorial:                              Tutorial.
  7659. * type of allocation:                    Example 1.
  7660. * underwrites:                           Overwrites and underwrites.
  7661. * unfreed allocations:                   Example 1.
  7662. * UNFREEDABORT:                          Environment.
  7663. * UNIX notes:                            UNIX notes.
  7664. * updates:                               Foreword.
  7665. * USEDEBUG:                              Environment.
  7666. * USEMMAP:                               Environment.
  7667. * using mpatrol:                         Using mpatrol.
  7668. * using with a debugger:                 Using with a debugger.
  7669. * utilities:                             Utilities.
  7670. * variable length arrays:                Dynamic memory allocations.
  7671. * variables, file scope:                 Static memory allocations.
  7672. * variables, local static:               Static memory allocations.
  7673. * variables, non-static local:           Stack memory allocations.
  7674. * variables, parameter:                  Stack memory allocations.
  7675. * VAX:                                   Profiling.
  7676. * virtual address:                       Virtual memory.
  7677. * virtual memory:                        Virtual memory.
  7678. * Vmalloc:                               Related software.
  7679. * warranty:                              Foreword.
  7680. * watch points:                          Virtual memory.
  7681. * watchmalloc(3x):                       Related software.
  7682. * Windows notes:                         Windows notes.
  7683. * Windows, Intel 80x86:                  Supported systems.
  7684. * Wipeout:                               Related software.
  7685. * write protection:                      Virtual memory.
  7686. * YAMD (Yet Another Malloc Debugger):    Related software.
  7687. * ZeroFault:                             Related software.
  7688.  
  7689.  
  7690. 
  7691. Tag Table:
  7692. Node: Top1252
  7693. Node: Foreword3227
  7694. Node: Overview8152
  7695. Node: Features10602
  7696. Node: Installation24770
  7697. Node: Integration29847
  7698. Node: Memory allocations34657
  7699. Node: Static memory allocations35334
  7700. Ref: Static memory allocations-Footnote-137795
  7701. Node: Stack memory allocations37837
  7702. Ref: Stack memory allocations-Footnote-139701
  7703. Node: Dynamic memory allocations39740
  7704. Ref: Dynamic memory allocations-Footnote-143080
  7705. Node: Operating system support43199
  7706. Node: Virtual memory45159
  7707. Ref: Virtual memory-Footnote-150919
  7708. Ref: Virtual memory-Footnote-251175
  7709. Ref: Virtual memory-Footnote-351351
  7710. Ref: Virtual memory-Footnote-451386
  7711. Ref: Virtual memory-Footnote-551445
  7712. Node: Call stacks and symbol tables51669
  7713. Ref: Call stacks and symbol tables-Footnote-155310
  7714. Ref: Call stacks and symbol tables-Footnote-255378
  7715. Ref: Call stacks and symbol tables-Footnote-355422
  7716. Ref: Call stacks and symbol tables-Footnote-455470
  7717. Node: Threads55576
  7718. Ref: Threads-Footnote-158313
  7719. Node: Using mpatrol58408
  7720. Node: Library behaviour59274
  7721. Ref: Library behaviour-Footnote-163970
  7722. Node: Logging and tracing64028
  7723. Node: General errors67074
  7724. Ref: General errors-Footnote-169688
  7725. Node: Overwrites and underwrites69812
  7726. Ref: Overwrites and underwrites-Footnote-176797
  7727. Ref: Overwrites and underwrites-Footnote-276860
  7728. Node: Using with a debugger76984
  7729. Ref: Using with a debugger-Footnote-185802
  7730. Ref: Using with a debugger-Footnote-285870
  7731. Ref: Using with a debugger-Footnote-386126
  7732. Node: Testing86215
  7733. Ref: Testing-Footnote-188952
  7734. Ref: Testing-Footnote-289133
  7735. Node: Library functions89192
  7736. Node: Utilities95930
  7737. Node: Profiling100950
  7738. Ref: Profiling-Footnote-1116729
  7739. Node: Improving performance116953
  7740. Ref: Improving performance-Footnote-1125647
  7741. Ref: Improving performance-Footnote-2125689
  7742. Ref: Improving performance-Footnote-3125766
  7743. Ref: Improving performance-Footnote-4125838
  7744. Node: How it works125882
  7745. Node: Examples130527
  7746. Node: Example 1132969
  7747. Ref: Example 1-Footnote-1154155
  7748. Node: Example 2154322
  7749. Node: Example 3158553
  7750. Node: Example 4165234
  7751. Node: Example 5168655
  7752. Ref: Example 5-Footnote-1172319
  7753. Node: Example 6172448
  7754. Node: Example 7174265
  7755. Node: Tutorial183687
  7756. Ref: Tutorial-Footnote-1197744
  7757. Ref: Tutorial-Footnote-2198026
  7758. Ref: Tutorial-Footnote-3198175
  7759. Ref: Tutorial-Footnote-4198312
  7760. Ref: Tutorial-Footnote-5198404
  7761. Node: Functions198684
  7762. Node: Environment223818
  7763. Node: Options241157
  7764. Node: Library performance248332
  7765. Node: Profiling file format250824
  7766. Node: Supported systems254072
  7767. Node: New system support260765
  7768. Node: New processor support261898
  7769. Node: New file format support262324
  7770. Node: Notes262714
  7771. Node: Generic notes264303
  7772. Ref: Generic notes-Footnote-1274453
  7773. Node: UNIX notes274652
  7774. Node: Amiga notes278148
  7775. Node: Windows notes280241
  7776. Node: Netware notes281051
  7777. Node: Related software282537
  7778. Ref: Related software-Footnote-1306083
  7779. Node: Function index306326
  7780. Node: Index308385
  7781. 
  7782. End Tag Table
  7783.